CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2011
    Posts
    59

    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.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Code not being generated for a constructor call

    Quote Originally Posted by adrian_h View Post
    Code:
    Y y(X(x));
    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.

  3. #3
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: Code not being generated for a constructor call


  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Code not being generated for a constructor call

    Quote Originally Posted by jnmacd View Post
    More or less:

    This is a function declaration:
    Code:
    Y y(X x);
    Now in C++, you are allowed to put parenthesis around parameter names, so it is equivalent to:

    Code:
    Y y(X(x));
    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:

    Code:
    Y y((X(x)));
    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.

  5. #5
    Join Date
    Oct 2011
    Posts
    59

    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

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Code not being generated for a constructor call

    Quote Originally Posted by adrian_h View Post
    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.

  7. #7
    Join Date
    Oct 2011
    Posts
    59

    Re: Code not being generated for a constructor call

    Oh yeah. Now I remember. Stupid C legacy.

    Thanks all!


    A

  8. #8
    Join Date
    Oct 2011
    Posts
    59

    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
  •  





Click Here to Expand Forum to Full Width

Featured