Initialization via ctor vs. initialization via assignment

Discussion in 'C++' started by Matthias Kaeppler, Jul 18, 2005.

  1. Hi,

    say I have an arbitrary class Bar:

    1 Bar a;
    2 Bar b(a);
    3 Bar c = a;

    In line 3, is the default ctor called for c _first_ and _then_ the
    assignment operator, or is c never default constructed and immediately
    initialized with a?

    My point is, for complex objects, is it likely that initialization via
    assignment is less efficient than via constructor calls? What is the
    recommended approach?

    --
    Matthias Kaeppler
     
    Matthias Kaeppler, Jul 18, 2005
    #1
    1. Advertising

  2. Matthias Kaeppler wrote:
    > Hi,
    >
    > say I have an arbitrary class Bar:
    >
    > 1 Bar a;
    > 2 Bar b(a);
    > 3 Bar c = a;
    >
    > In line 3, is the default ctor called for c _first_ and _then_ the
    > assignment operator, or is c never default constructed and immediately
    > initialized with a?
    >
    > My point is, for complex objects, is it likely that initialization via
    > assignment is less efficient than via constructor calls? What is the
    > recommended approach?
    >


    2 & 3 mean exactly the same thing. BTW, one way of answering this
    questing is by writing a 30 liner like so.

    #include <iostream>
    #include <ostream>

    struct A
    {
    A()
    {
    std::cout << "A default\n";
    }

    A( const A & )
    {
    std::cout << "A copy\n";
    }

    A & operator= ( const A & )
    {
    std::cout << "A operator =\n";
    return * this;
    }

    };


    int main()
    {
    std::cout << "A x;\n";
    A x;

    std::cout << "A y( x );\n";
    A y( x );

    std::cout << "A z = x;\n";
    A z = x;
    }
     
    Gianni Mariani, Jul 18, 2005
    #2
    1. Advertising

  3. Matthias Kaeppler wrote:
    > say I have an arbitrary class Bar:
    >
    > 1 Bar a;
    > 2 Bar b(a);
    > 3 Bar c = a;
    >
    > In line 3, is the default ctor called for c _first_ and _then_ the
    > assignment operator,


    No.

    > or is c never default constructed and immediately
    > initialized with a?


    'c' is constructed from 'a' via the copy c-tor. Since 'a' and 'c' are
    of the same type, the case 3 is the same as the case 2.

    > My point is, for complex objects, is it likely that initialization via
    > assignment is less efficient than via constructor calls? What is the
    > recommended approach?


    There is no assignment involved in construction (unless you make it so).

    V
     
    Victor Bazarov, Jul 18, 2005
    #3
    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. Apricot
    Replies:
    4
    Views:
    529
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    488
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    488
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    411
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. puzzlecracker
    Replies:
    8
    Views:
    430
    James Kanze
    Apr 15, 2008
Loading...

Share This Page