|
-
October 18th, 2011, 12:36 AM
#1
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
Last edited by adrian_h; October 18th, 2011 at 08:55 AM.
Reason: Added code blocks.
-
October 18th, 2011, 07:00 AM
#2
Re: Code not being generated for a constructor call
 Originally Posted by adrian_h
Be alert for C++'s most vexing parse.
-- Scott Meyers
Are you sure that's a class object you are instantiating there...?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 18th, 2011, 07:07 AM
#3
Re: Code not being generated for a constructor call
-
October 18th, 2011, 07:59 AM
#4
Re: Code not being generated for a constructor call
 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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
October 18th, 2011, 08:50 AM
#5
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
-
October 18th, 2011, 08:53 AM
#6
Re: Code not being generated for a constructor call
 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.
-
October 18th, 2011, 10:51 AM
#7
Re: Code not being generated for a constructor call
Oh yeah. Now I remember. Stupid C legacy. 
Thanks all! 
A
-
October 18th, 2011, 10:55 AM
#8
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|