Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialization via ctor vs. initialization via assignment

Reply
Thread Tools

Initialization via ctor vs. initialization via assignment

 
 
Matthias Kaeppler
Guest
Posts: n/a
 
      07-18-2005
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
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      07-18-2005
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;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-18-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
copy ctor vs default ctor subramanian100in@yahoo.com, India C++ 2 08-15-2007 10:49 AM
ctor/dtor calls and ctor init seq Grizlyk C++ 8 11-29-2006 06:35 AM
Templates, copy ctor and type-conversion ctor NVH C++ 8 07-06-2006 07:19 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM



Advertisments