AClass ac=AClass("name",23); VS. AClass ac("name", 23);

Discussion in 'C++' started by Alfred E Neuman, Nov 4, 2003.

  1. I've read that... AClass ac = AClass( "name", 23 );

    is less efficient than... AClass ac( "name", 23 );

    because the first has to create a temporary object which is then
    assigned to ac.

    However, I can't seem to prove this true. I've created a class
    containing the following constructors and overloaded equal operator.

    /* default constructor */ AClass();
    /* main constructor */ AClass( const char *, int );
    /* copy constructor */ AClass( const AClass & );
    /* overloaded = operator */ AClass operator=( const AClass & );

    I placed cout statements in each to show program execution flow.

    AClass ac = AClass( "name", 23 ); executes my main constructor once
    and a matching destructor call once. I never see any other
    constructor or destructor calls.

    However,

    AClass ac;
    ac = AClass( "name", 23 );

    obviously calls two constructors; the default for object ac and then
    the main constructor for the temporary object which is then assigned
    to ac. Matching destructor calls are made as well. This makes sense
    because of the two statement setup.

    My confusion is that I see no difference between

    AClass ac = AClass( "name", 23 ); and
    AClass ac( "name", 23 );

    What am I missing here? Was the information I read wrong? Is there
    some kind of compiler optimization going on? I've tried this under
    Microsoft Visual C++, Borland C++, and gcc on Linux, all with the same
    results. I'm stumped.
     
    Alfred E Neuman, Nov 4, 2003
    #1
    1. Advertising

  2. Alfred E Neuman

    Ron Natalie Guest

    "Alfred E Neuman" <> wrote in message news:...
    > I've read that... AClass ac = AClass( "name", 23 );
    >
    > is less efficient than... AClass ac( "name", 23 );
    >
    > because the first has to create a temporary object which is then
    > assigned to ac.


    There is NEVER any assignment in either of the cases. What there
    is logically in the first case is copy construction from the temporary.
    However, no compiler I've ever come accross fails to optimize away
    the temporary. In both cases ac is built directly from the two args
    you provided.
     
    Ron Natalie, Nov 4, 2003
    #2
    1. Advertising

  3. Here goes several extra words to Ron's reply.

    "Alfred E Neuman" <> wrote in message
    news:...
    [...]
    > My confusion is that I see no difference between
    >
    > AClass ac = AClass( "name", 23 ); and
    > AClass ac( "name", 23 );

    The first case requires AClass to have the properly declared copy
    constructor. Properly declared in this case means that it shall be
    accessible in the context and its, constructor's, argument shall have const
    (or const volatile) AClass& form. The second is because of the temporary
    been involved in. As you were already told it is not necessary that the copy
    constructor would be called in this case.
    The second case does not require an AClass copy constructor declaration to
    compile and link.

    --
    Michael Kochetkov.
     
    Michael Kochetkov, Nov 4, 2003
    #3
  4. Ok, so the answer boils down to this being a common compiler
    optimization implemented into virtually all C++ compilers.

    Thanks for clearing this up!
     
    Alfred E Neuman, Nov 4, 2003
    #4
    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. Amit_Basnak
    Replies:
    3
    Views:
    344
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Jan 11, 2007
  2. cutecutemouse
    Replies:
    2
    Views:
    387
    cutecutemouse
    Jul 13, 2008
  3. Utku Altinkaya
    Replies:
    0
    Views:
    333
    Utku Altinkaya
    Aug 17, 2008
  4. ssb
    Replies:
    58
    Views:
    1,716
    Chris M. Thomasson
    Sep 14, 2009
  5. trans.  (T. Onoma)
    Replies:
    6
    Views:
    147
    trans. (T. Onoma)
    Oct 1, 2004
Loading...

Share This Page