H
hill.liu
Hi,
I stuck into this problem that I can't figure it out.
Here is the class definition:
class ctest {
public:
ctest(void) { cout << "ctest default constor" << endl; };
ctest(ctest& c) { cout <<"ctest copy constr" << endl; };
ctest(int a) { cout <<"ctest int constor" <<endl; };
ctest& operator= (ctest& a) { cout << "ctest copy assignment" <<
endl; return *this; };
operator int() { cout << "call operator int()" <<endl; return 20; }
};
int main(void)
{
ctest cc = ctest();
}
And it outputs as following:
ctest default constor
call operator int()
ctest int constor
I wonder why it will invoke "operator int()" and call "constructor
with int as argument".
Thanks
Brad
I stuck into this problem that I can't figure it out.
Here is the class definition:
class ctest {
public:
ctest(void) { cout << "ctest default constor" << endl; };
ctest(ctest& c) { cout <<"ctest copy constr" << endl; };
ctest(int a) { cout <<"ctest int constor" <<endl; };
ctest& operator= (ctest& a) { cout << "ctest copy assignment" <<
endl; return *this; };
operator int() { cout << "call operator int()" <<endl; return 20; }
};
int main(void)
{
ctest cc = ctest();
}
And it outputs as following:
ctest default constor
call operator int()
ctest int constor
I wonder why it will invoke "operator int()" and call "constructor
with int as argument".
Thanks
Brad