Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is a c++ constructor not called?

I have a situation where no constructor appears to be called:

#include <iostream>

using namespace std;

int main ()
{
    class yoyo
    {
        public:
        int i;
        yoyo()
        {
            i = 0;
            cout << "defaultly initialized to 0" << endl;
        }
        yoyo (int j) : i(j)
        {
            cout << "initialized to " << j << endl;
        }
    };

    int i;

    yoyo a;
    cout << "Hello1, i: " << a.i << endl;

    yoyo b(5);
    cout << "Hello2, i: " << b.i << endl;

    yoyo c = b;                                   /* 1 */
    cout << "Hello3, i: " << c.i << endl;

    return 0;
}

Output is:

defaultly initialized to 0
Hello1, i: 0
initialized to 5
Hello2, i: 5
Hello3, i: 5

(Note: nothing between Hello2 and Hello3)

If I change the program to read as follows:

#include <iostream>

using namespace std;

int main ()
{
    class yoyo
    {
        public:
        int i;
        yoyo()
        {
            i = 0;
            cout << "defaultly initialized to 0" << endl;
        }
        yoyo (int j) : i(j)
        {
            cout << "initialized to " << j << endl;
        }
    };

    int i;

    yoyo a;
    cout << "Hello1, i: " << a.i << endl;

    yoyo b(5);
    cout << "Hello2, i: " << b.i << endl;

    yoyo c; c = b;                                  /* 1 */
    cout << "Hello3, i: " << c.i << endl;

    return 0;
}

(The only difference is in he line marked by /* 1 */)

The output now is:

defaultly initialized to 0
Hello1, i: 0
initialized to 5
Hello2, i: 5
defaultly initialized to 0
Hello3, i: 5

Now there is a constructor call between Hello2 and Hello3. My question is, why is there no (visible) constructor call in the first case?

like image 716
user2518270 Avatar asked Jun 25 '13 00:06

user2518270


People also ask

How do you call a constructor in C#?

To call one constructor from another within the same class (for the same object instance), C# uses a colon followed by the this keyword, followed by the parameter list on the callee constructor's declaration. In this case, the constructor that takes all three parameters calls the constructor that takes two parameters.

Which constructor is called first in C#?

In C# terms, the base constructor is executed first.

Why constructor is used in C#?

A constructor is a special method of the class which gets automatically invoked whenever an instance of the class is created. Like methods, a constructor also contains the collection of instructions that are executed at the time of Object creation.

What is constructor in C# with example?

In C#, a constructor is called when we try to create an object of a class. For example, Car car1 = new Car();


1 Answers

In the case of

yoyo c = b;

it's the copy constructor that is called.

And in the case of

yoyo c; c = b;

it's the copy assignment operator that is called.

If you don't provide any of them, the compiler will generate default versions for you.


If you want to create your own copy constructor, it could look like this:

yoyo(const yoyo& other)
    : i(other.i)
    { std::cout << "copy constructor initialized\n"; }

The copy assignment operator looks like this:

yoyo& operator=(const yoyo& other)
    {
        i = other.i;
        return *this;
    }

Both of them defined inside the class definition of course.

like image 164
Some programmer dude Avatar answered Oct 05 '22 10:10

Some programmer dude