Temporary objects as arguments to constructor calls

Discussion in 'C++' started by szaki@colbud.hu, Jun 21, 2005.

  1. Guest

    Hi,

    I have tested the piece of code below on Intel ICC and GCC compilers,
    both produce the same result.

    --------------------
    CODE:
    --------------------

    #include <iostream>
    using namespace std;

    class X
    {
    public:
    X() {cout << "X constructor" << endl;}
    };

    class B
    {
    public:
    B(const X &x) {cout << "B constructor" << endl;}
    };

    int main()
    {
    cout << "step 1" << endl;
    B b1(X());
    cout << "step 2" << endl;
    B b2=B(X()); //works
    return 0;
    }

    --------------
    RESULT:
    --------------

    step 1
    step 2
    X constructor
    B constructor

    --------------

    That is, in step 1 constructors are NOT CALLED! I cannot see why. Can
    anybody explain it?

    Thank you

    Zoltan
     
    , Jun 21, 2005
    #1
    1. Advertising

  2. wrote:
    > I have tested the piece of code below on Intel ICC and GCC compilers,
    > both produce the same result.
    >
    > --------------------
    > CODE:
    > --------------------
    >
    > #include <iostream>
    > using namespace std;
    >
    > class X
    > {
    > public:
    > X() {cout << "X constructor" << endl;}
    > };
    >
    > class B
    > {
    > public:
    > B(const X &x) {cout << "B constructor" << endl;}
    > };
    >
    > int main()
    > {
    > cout << "step 1" << endl;
    > B b1(X());


    'b1' is a function. The statement above is a declaration of a function.
    Read the FAQ.

    > cout << "step 2" << endl;
    > B b2=B(X()); //works
    > return 0;
    > }
    >
    > --------------
    > RESULT:
    > --------------
    >
    > step 1
    > step 2
    > X constructor
    > B constructor
    >
    > --------------
    >
    > That is, in step 1 constructors are NOT CALLED! I cannot see why. Can
    > anybody explain it?
     
    Victor Bazarov, Jun 21, 2005
    #2
    1. Advertising

  3. Guest

    Hm, I see, thanks. I still think it is weird, but probably it has its
    rationale.

    Victor Bazarov írta: > wrote:
    > > I have tested the piece of code below on Intel ICC and GCC compilers,
    > > both produce the same result.
    > >
    > > --------------------
    > > CODE:
    > > --------------------
    > >
    > > #include <iostream>
    > > using namespace std;
    > >
    > > class X
    > > {
    > > public:
    > > X() {cout << "X constructor" << endl;}
    > > };
    > >
    > > class B
    > > {
    > > public:
    > > B(const X &x) {cout << "B constructor" << endl;}
    > > };
    > >
    > > int main()
    > > {
    > > cout << "step 1" << endl;
    > > B b1(X());

    >
    > 'b1' is a function. The statement above is a declaration of a function.
    > Read the FAQ.
    >
    > > cout << "step 2" << endl;
    > > B b2=B(X()); //works
    > > return 0;
    > > }
    > >
    > > --------------
    > > RESULT:
    > > --------------
    > >
    > > step 1
    > > step 2
    > > X constructor
    > > B constructor
    > >
    > > --------------
    > >
    > > That is, in step 1 constructors are NOT CALLED! I cannot see why. Can
    > > anybody explain it?
     
    , Jun 22, 2005
    #3
  4. wrote:
    > Hm, I see, thanks. I still think it is weird, but probably it has its
    > rationale.


    (a) Please don't top-post

    (b) If you _are_ interested in the rationale, it's been discussed
    quite a few times, and I am fairly certain you can find it either
    on the web or in the news archives. Look for "what looks like
    a declaration is a declaration".

    > Victor Bazarov írta: > wrote:
    >
    >>>I have tested the piece of code below on Intel ICC and GCC compilers,
    >>>both produce the same result.
    >>> [...]
    >>>int main()
    >>>{
    >>> cout << "step 1" << endl;
    >>> B b1(X());

    >>
    >>'b1' is a function. The statement above is a declaration of a function.
    >>Read the FAQ.
    >>
    >>
    >>> cout << "step 2" << endl;
    >>> B b2=B(X()); //works
    >>> return 0;
    >>>}
    >>>


    V
     
    Victor Bazarov, Jun 22, 2005
    #4
  5. Guest

    Victor Bazarov írta: > wrote:
    > > Hm, I see, thanks. I still think it is weird, but probably it has its
    > > rationale.

    >
    > (a) Please don't top-post
    >
    > (b) If you _are_ interested in the rationale, it's been discussed
    > quite a few times, and I am fairly certain you can find it either
    > on the web or in the news archives. Look for "what looks like
    > a declaration is a declaration".
    >
    > > Victor Bazarov írta: > wrote:
    > >


    Dear Victor

    (a) sorry about that, I haven't used much newsgroups, so I don't know
    the ethics. But as you can see, I looked up what top-posting means.

    (b) Yes, I am interested, unless it requires thorough knowledge of the
    theory of C++ compilers. I'll look it up on the web. In fact, before
    posting I had searched the web, the only thing I found mentioned this
    'function declaration vs. variable initialization' ambiguity but only
    in the context of empty constructors (e.g why string s(); does not
    work).

    (c) Since my last post I've found out that although B b1(X()); does not
    work, B b1((X())); does, which makes the story even more weird.

    Regards

    Zoltan
     
    , Jun 24, 2005
    #5
  6. wrote:
    > [...]
    > (c) Since my last post I've found out that although B b1(X()); does not
    > work, B b1((X())); does, which makes the story even more weird.


    Since you've found the "ambiguity" thing, you must have seen how C++
    resolves it. I actually mentioned it. What looks like a declaration *is*
    a declaration. Now, according to C++ grammar rules, if the identifier is
    followed by a parentheses after a type, it could be either a declaration
    or an initialiser. A declaration would be of a function, and the format
    of the formal argument list is quite strict: either empty or a list of
    declarations. As soon as the compiler encounters the second opening
    parenthesis ("B b1 ( _(_ ") then it sees that what's inside the first,
    the outermost set of parentheses is not a list of formal arguments but
    instead an expression. It makes a conclusion that what it has here is
    an object definition/initialisation.

    V
     
    Victor Bazarov, Jun 24, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honne Gowda A
    Replies:
    2
    Views:
    880
    Karl Heinz Buchegger
    Oct 31, 2003
  2. Andy Buckley
    Replies:
    5
    Views:
    337
    Victor Bazarov
    Aug 26, 2004
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,246
  4. Replies:
    7
    Views:
    3,224
    James Kanze
    Feb 12, 2008
  5. Marcelo De Brito

    Copy Constructor And Temporary Objects

    Marcelo De Brito, Mar 1, 2009, in forum: C++
    Replies:
    2
    Views:
    501
    Kai-Uwe Bux
    Mar 1, 2009
Loading...

Share This Page