Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ constructor calling and object creation

class Test{
    public :
        int x;  
        Test()
        {
            x = 0;
            cout<<"constructor with no arguments called"<<endl;
        }
        Test(int xx)
        {
            x = xx;
            cout<<"constructor with single int argument called"<<endl;
        }
        
};


int main()
{
    Test a(10);
    Test aa = 10;
}

output: Program compiles and outputs

constructor with single int argument called

constructor with single int argument called

But now

class Test{
    public :
        int x;  
        Test()
        {
            x = 0;
            cout<<"constructor with no arguments called"<<endl;
        }
        Test(int xx)
        {
            x = xx;
            cout<<"constructor with single int argument called"<<endl;
        }
        
        Test( Test& xx)
        {
            x = xx.x;
            cout<<"copy constructor called"<<endl;
        }
        
        
};


int main()
{
    Test a(10);
    Test aa = 10;
}

compilation fails.

constructorinvokings.cc:36:7: error: no viable constructor copying variable of type 'Test'
        Test aa = 10;
             ^    ~~
constructorinvokings.cc:23:3: note: candidate constructor not viable: no known conversion from 'Test' to 'Test &' for 1st
      argument
                Test( Test& xx)
                ^
1 error generated.

I am new to C++.

Aren't Test a(10) and Test aa = 10; identical ?

why is the addition of copy constructor conflicting with Test aa=10?

if I modify Test(Test& xx) to Test(const Test& xx) it is working. But why is the compiler checking for copy constructor signature when we are trying to call constructor with integer argument.

Please clarify

Thanks in advance.

like image 892
Vineel Kumar Reddy Avatar asked Dec 22 '22 08:12

Vineel Kumar Reddy


1 Answers

All of the answers you've gotten so far are overly complicated and/or slightly misleading.

In the first construction/initialization:

T a(10);

The very obvious thing happens. The second construction/initialization is more interesting:

T aa = 10;

This is equivalent to:

T aa(T(10));

Meaning that you create a temporary object of type T, and then construct aa as a copy of this temporary. This means that the copy constructor is called.

C++ has a default copy constructor that it creates for a class when you have none explicitly declared. So even though the first version of class T has no copy constructor declared, there still is one. The one the compiler declares has this signature T(const T &).

Now in your second case where you declare something that looks like a copy constructor, you make the argument a T &, not a const T &. This means that the compiler, when trying to compile the second expression, tries to use your copy constructor, and it can't. It's complaining that the copy constructor you declared requires a non-const argument, and the argument it's being given is const. So it fails.

The other rule is that after the compiler has converted your initialization to T aa(T(10)); it is then allowed to transform it to T aa(10);. This is called 'copy elision'. The compiler is allowed, in certain circumstances, to skip calls to the copy constructor. But it may only do this after it verifies that the expression is correctly formed and doesn't generate any compiler errors when the copy constructor call is still there. So this is an optimization step that may affect exactly which parts of a program run, but cannot affect which programs are valid and which ones are in error (at least from the standpoint of whether or not they compile).

like image 135
Omnifarious Avatar answered Dec 24 '22 02:12

Omnifarious