Output of the code is 25! How does the object of class opp initialize its private member?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include<iostream>
using namespace std;
class constTest{
private:
int a;
public:
void show();
constTest(int);
};
constTest::constTest(int m = 0)
{
a = m;
}
void constTest::show(){cout<<a<<endl;}
int main()
{
constTest opp(6*6);
opp=5*5;
opp.show();
return 0;
}
|
Last edited on
Single-argument non-explicit constructors participate in implicit conversions: it is roughly equivalent to opp = constTest(5*5);
PS: put your default arguments on the declaration (inside the class); clang for example refuses to compile this as written.
Last edited on
Thank Cubbi. And hence the explicit keyword!