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