cplusplus.co.il

Problematic declaration syntax

Posted on: 20/08/2009

This question was written by a friend who luckily let me use it. It presents a very common pitfall of C++, regarding the syntax of decleration statements.

In the following code, what would be the type of b?

struct A {
    A (int i) {}
};

struct B {
    B (A a) {}
};

int main () {
    int i = 1;
    B b(A(i));
    return 0;
}

Hint: you will be surprised! Try printing inside B’s constructor.

2 Responses to "Problematic declaration syntax"

A few good replies to this question can now be found on stackoverflow:
http://stackoverflow.com/questions/1379932/trick-question-regarding-declaration-syntax-in-c

Eh, the most vaxine parse 😀

Leave a comment

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 27 other subscribers

Categories