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.