Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I have to use initialization list to use constructor delegation in c++?

I know we have to use an initialization list to use constructor delegation in c++. But why we can’t use it in another way? For example in the code below, constructor delegation is not working. It is not setting the health=pHealth.But the court << "I am the main constructor" is printing on the console.It means the constructor has been called but not set the value to the health parameters.

code-

#include <iostream>


using namespace std;

class Player{
    int health;
    public:
    Player(int pHealth){
        health=pHealth;
        cout << "I am main constructor";
    }
    Player(){
        Player(40);
    }

    void check(){
        cout << health;
    }

};

int main(int argc, char const *argv[])
{
    Player x;
    x.check();
    return 0;
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your program is creating two objects.

  1. using x
  2. using "Player(40);" inside constructor
    Sample:
#include <iostream>
using namespace std;
class Player
{
                int health;
        public:
                Player(int pHealth)
                {
                        cout << "One argument constructor this: " << this << "\n";
                        health=pHealth;
                }
                virtual ~Player()
                {
                        cout << "destructor this: " << this << "\n";
                }
                Player()
                {
                        cout << "Default constructor this: " << this << "\n";
                        Player(40);     // Create new object using one argument constructor.
                }
                void check()
                {
                        cout << "health: " << health << " this: " << this << "\n";
                }
};
int main(int argc, char const *argv[])
{
        Player x;
        x.check();
        return 0;
}

Output:

$ g++  -g -Wall 73596563.cpp -o ./a.out
$  ./a.out
Default constructor this: 0xffffcc10
One argument constructor this: 0xffffcbd0
destructor this: 0xffffcbd0
health: -13184 this: 0xffffcc10
destructor this: 0xffffcc10

Hence 0xffffcbd0 being created inside that default constructor.
Hence updated your code same using:

                Player():health(40)
                {
                        cout << "Default constructor this: " << this << "\n";
                        // Player(40);  // Create new object using one argument constructor.
                }

Output obtained after above update:

$ ./a.out
Default constructor this: 0xffffcc10
health: 40 this: 0xffffcc10
destructor this: 0xffffcc10

Few more comment:

Player x;
x.check();
// Call constructor without creating object.
Player();
Player(2003);

Hence related output:

$ ./a.out
Default constructor this: 0xffffcbf0
health: 40 this: 0xffffcbf0
Default constructor this: 0xffffcc00
destructor this: 0xffffcc00
One argument constructor this: 0xffffcc10
destructor this: 0xffffcc10
destructor this: 0xffffcbf0

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading