Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing private member variables of a class

I apologize in advance because some of my verbiage may not be 100% correct.

I will have a class like this:

class ClassName {
private:
    AnotherClass class2;
public:
  ClassName();
  ~ClassName();
...

In the constructor of this class, among other things, I put the line

ClassName::ClassName() {
    AnotherClass class2; 
}

Which is how I thought you were supposed to initialize objects in C++, however I was noticing (through GDB) that two AnotherClass objects were being created. Once on the Constructor definition then again on my initialization line. What is the reasoning behind this? What if I wanted to use a more complicated constructor like AnotherClass(int a, int b), would it create a temporary object then create the correct one shortly after?

like image 227
Maeve Kennedy Avatar asked Sep 30 '15 07:09

Maeve Kennedy


People also ask

How do you initialize a private variable in class?

To initialize we have to use the class name then scope resolution operator (::), then the variable name. Now we can assign some value.

How do you initialize a private variable in Java?

We have used the getter and setter method to access the private variables. Here, the setter methods setAge() and setName() initializes the private variables. the getter methods getAge() and getName() returns the value of private variables.

How do you initialize a member variable?

The member initializer list is inserted after the constructor parameters. It begins with a colon (:), and then lists each variable to initialize along with the value for that variable separated by a comma.

Can we initialize data members in a class?

In C++, class variables are initialized in the same order as they appear in the class declaration. Consider the below code. The program prints correct value of x, but some garbage value for y, because y is initialized before x as it appears before in the class declaration.


2 Answers

AnotherClass class2; creates another local object inside the constructor body, that gets destroyed at the end of the body. That is not how class members are initialized.

Class members are initialized before the constructor body in the member initializer list between the constructor signature and body, starting with a :, like so:

ClassName::ClassName() :
    class2(argumentsToPassToClass2Constructor),
    anotherMember(42) // just for example
{
    /* constructor body, usually empty */
}

If you don't want to pass any arguments to the class2 constructor you don't have to put it in the initializer list. Then its default constructor will be called.

If you simply want to call the default constructor on all of your class members, you can (and should) omit the constructor altogether. The implicitly generated default constructor will do just what you wanted.

like image 139
emlai Avatar answered Oct 04 '22 09:10

emlai


What you are doing in your constructor is creating another variable, local only inside the constructor.

Actually, if you do nothing, the default constructor in AnotherClass will be called for the class2 object.

If you want to be explicit, you can use a constructor initializer list:

ClassName::ClassName()
    : class2()
{
}

This last method is also the way you call a specific constructor with arguments in AnotherClass, if you need to do that.

like image 29
Some programmer dude Avatar answered Oct 04 '22 10:10

Some programmer dude