Code not being generated for a constructor call
Hi,
Strange thing. I was trying to do some code and it wasn't working. Tried it in several different ways and still no luck. Finally, tried to make a very small case where it was failing, and I got this:
Code:
#include <iostream>
using namespace std;
class X
{
public:
int x;
X(int x) : x(x) { cout << "X(int x=" << x << ")" << endl; }
X(X const & x) : x(x.x) { cout << "X(X const & x={" << x.x << "})" << endl; }
};
class Y : public X
{
public:
Y(int x) : X(x) { cout << "Y(int x=" << x << ")" << endl; }
Y(X const & x) : X(x) { cout << "Y(X const & x={" << x.x << "})" << endl; }
};
int main()
{
cout << "1----" << endl;
X x(3); // code generated
cout << "2----" << endl;
Y y(X(x)); // code not generated. Expecting call to X(X const &) constructor then Y(X const &)
cout << "3----" << endl;
X x1(x); // code generated
cout << "4----" << endl;
Y y1(x1); // code generated
cout << "5----" << endl;
return 0;
}
Expecting to get output:
Code:
1----
X(int x=3)
2----
X(X const & x={3})
Y(X const & x={3})
3----
X(X const & x={3})
4----
X(X const & x={3})
Y(X const & x={3})
5----
but instead get:
Code:
1----
X(int x=3)
2----
3----
X(X const & x={3})
4----
X(X const & x={3})
Y(X const & x={3})
5----
Stepping through the code, it doesn't stop on Y y(X(x)); yet by explicitly making a variable of type X and passing it to constructor Y(X const &) it works. Only difference is that #2 uses a temporary var and #4 uses a standard var. Looking at the disassembly, it shows no code generated for that line.
This is very confusing :(. Any ideas? I'm thinking it could be the compiler puking. If someone could compile this on their system and verify the results, that would be grand.
Thanks.
A
Using g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1 with Eclipse Version: 3.5.1 Build id: M20090917-0800
Re: Code not being generated for a constructor call
Quote:
Originally Posted by
adrian_h
Quote:
Be alert for C++'s most vexing parse.
--Scott Meyers
Are you sure that's a class object you are instantiating there...? :wave:
Re: Code not being generated for a constructor call
Re: Code not being generated for a constructor call
Quote:
Originally Posted by
jnmacd
More or less:
This is a function declaration:
Now in C++, you are allowed to put parenthesis around parameter names, so it is equivalent to:
The problem is that this can be both a function declaration, or an object instantiation. Unfortunately, the function declaration has "right of way"
To "fix" this, you can add an extra disambiguate parenthesis:
Now that "X(x)" is between parenthesis, there is no way for the above to be a function declaration, therefore it must be an object instantiation.
Re: Code not being generated for a constructor call
That's interesting. But isn't it illegal to declare a function in a function?
A
Re: Code not being generated for a constructor call
Quote:
Originally Posted by
adrian_h
That's interesting. But isn't it illegal to declare a function in a function?
A
Define, yes. Declare, no.
Not that anyone should do such a thing, but it's legal.
Re: Code not being generated for a constructor call
Oh yeah. Now I remember. Stupid C legacy. :(
Thanks all! :)
A
Re: Code not being generated for a constructor call
OT but how come my editor doesn't have the buttons for formatting?
Using Google Chrome 12.0.742.124