0

I know we have to use an initialization list to use constructor delegation. But why can't we 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 cout << "I am the main constructor" is printing on the console. It means the constructor has been called, but is not setting 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;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sonet Adhikary
  • 466
  • 1
  • 4
  • 8
  • 3
    `Player(40);` creates a temporary object that goes away as soon as the`;` is encountered. – NathanOliver Sep 04 '22 at 03:15
  • Dupe: [Is constructor delegation the same as calling the constructor?](https://stackoverflow.com/questions/72729916/is-constructor-delegation-the-same-as-calling-the-constructor). Also, refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of SO related question for this. This is also explained in any beginner [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Sep 04 '22 at 03:35
  • "*But why can't we use it in another way?*" - because the C++ standard doesn't define constructor delegation any other way. You MUST use the initialization list, there is no other option. Simple as that. – Remy Lebeau Sep 04 '22 at 05:12

1 Answers1

-1

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