This code :
class A {
public:
int _a;
A():_a(0) { cout << "A default - ctor, ";}
A(int i):_a(i) { cout << "A int - ctor, ";}
operator int() { cout << "A int - operator, "; return 33;}
virtual ~A() { cout << "A dtor, ";}
};
int main() {
A* a = new A(99);
cout << *a << endl;
return 0;
}
I expected the output to be : A int-operator, A int-ctor, 33
but the true output is : A int-ctor, A int-operator, 33
First you call "new A(99)"
, which invokes "A(int i)"
, which prints "A int - ctor, "
.
The following line calls "cout << *a"
, which invokes "operator int()"
, which prints "A int - operator, "
.
Then the line complete with the "cout << [result] << endl"
, which prints the result (33) and a newline.
So "A int - ctor, "
+ "A int - operator, "
+ "33"
+ "endl"
is "A int - ctor, A int - operator, 33"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.