Confusing code
-
#include using namespace std; struct C; struct D { void operator*(D) { cout << "one\n"; } } C; struct E { void operator*(E) { cout << "two\n"; } } F; struct F; int main(){ C* C; F* F; return 0; } I got the above code from a C++ site. This code prints "one" "two" on the screen. Can anybody explain why is it printing so? :confused: :confused: :confused: void (*p[10]) (void (*)());
-
#include using namespace std; struct C; struct D { void operator*(D) { cout << "one\n"; } } C; struct E { void operator*(E) { cout << "two\n"; } } F; struct F; int main(){ C* C; F* F; return 0; } I got the above code from a C++ site. This code prints "one" "two" on the screen. Can anybody explain why is it printing so? :confused: :confused: :confused: void (*p[10]) (void (*)());
C* C looks like declaration of pointer to C, but it isn't. It's a call to operator* overloaded in struct D. To make code clearer, put the space between first 'C' and asterisk - you'll see that this is an expression, not the declaration: C * C; This also applies to F* F (or rather F * F). Tomasz Sowinski -- http://www.shooltz.com.pl
-
C* C looks like declaration of pointer to C, but it isn't. It's a call to operator* overloaded in struct D. To make code clearer, put the space between first 'C' and asterisk - you'll see that this is an expression, not the declaration: C * C; This also applies to F* F (or rather F * F). Tomasz Sowinski -- http://www.shooltz.com.pl