Problematic declaration syntax

August 20th, 2009 § 2 Comments

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 declaration 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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What’s this?

You are currently reading Problematic declaration syntax at 0x.

meta