Question about accessing class private variables


Hi guys!

Trying to study here and I have come across something my C++ book is not mentioning about class private variables.

My code:

.H

#ifndef HANNAH_H
#define HANNAH_H

class Hannah
{
public:
Hannah(int);

void printCrap();

private:
int c;

};


#endif // HANNAH_H

=====

CPP

#include <iostream>
#include "Hannah.h"

Hannah::Hannah(int num)
:c(num) //What is it called when code is put here? <- HERE!!!
{

}

=====

Look at where it says

:c(num) //what is this?

Well that question about says it all. Specifically, though, when you put something after the parameters but before the brackets on a class function, what is that called? I actually understand completely what is going on with the code and how to work it I just do not know the right definition for it. I want to study it more but I do not know what it is called.

Thank you in advance.








Last edited on
It's called a member initializer list.
https://en.cppreference.com/w/cpp/language/constructor

It's not specifically about private member variables, but about member variables in general (i.e., they could be public).

BTW, remember to use "code tags" when posting code:

[code]
your code goes here
[/code]


Last edited on
Ah! Good to know thanks!
The C++ standards committee is working overtime to make sure we can hide a lot of classic emojis in the code with legit looking syntax
make in c into a p, for example, and you can sneak a :P in there.
Registered users can post here. Sign in or register to post.