简体   繁体   中英

Intializing class member variables in constructor

I recently started working on C++, I observed one strange behaviour while initializing the member variables through the initializer list.

//Demo.h
#ifndef DEMO
#define DEMO
#include <iostream>
class Demo
{
public:
    Demo();
    explicit Demo(int x);
};

#endif 

//Demo.cpp
#include "Demo.h"
Demo::Demo()
{
    std::cout << "Demo()" << std::endl;
}
Demo::Demo(int x)
{
    std::cout << "Demo(int x)" << std::endl;
}

//Test.cpp
#include "Demo.h"
class Test
{
public:
    Test()
    // : d_(123)
    {
    }

private:
    Demo d_;
};
int main(int argc, char const *argv[])
{
    Test t;
    return 0;
}

In the above code snippet, I created one object for class Demo( d_ ), when I run the program Demo() is printing. But if I uncomment the commented code( d_(123) ) I am getting Demo(int x) as output.

The first scenario is clear to me, but the second one I am not able to understand? Edit: My question is I already created an object(member variables In-class Test), but again I am passing the integer argument in the initializer list. So is it ok to pass, because the object already created and again trying to initialize it?

You do not initialize a data member when you just declare it in the body of the class. Actually you do not create anything at all when you write a definition for the class, it is just a blueprint for creation of future instances of the class. Later, when you make an object of the class type, the object is created by means of an according constructor, and it is only then we have initialization of the data members.

After you've defined the class, you instantiate it as in

Test t; // an object created, default constructor is used 

In this case, there's an object t created, using the default constructor of the Test . This default constructor initializes class' data member d_ of type Demo using Demo 's constructor that takes an int .

When you construct an object, all data members in the member initializer list are still to be instantiated (they aren't default constructed, they are still to be constructed in some way) . That's the beauty of the member initializer list .

So, an object can be default constructed, but it can be constructed using other constructor (non-default) , in which case the default constructor is not even touched.

Note, if you were to avoid the use of the member initializer list and initialed data members in the body of the constructor, data members would have been default constructed first, and then reassigned with new values (in the body of the constructor) .

The first scenario is clear to me, but the second one I am not able to understand?

In case you uncomment the member initialiser list, you are passing an argument to the constructor of Demo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM