473,388 Members | 1,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Initialization via ctor vs. initialization via assignment

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
Jul 23 '05 #1
2 1602
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;
}
Jul 23 '05 #2
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
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Michael McKnerney | last post by:
Hi, It seems I can influence how a base class is initialized beyond the 'normal' manner and I was wondering if someone can tell me why this. Here's my example. class A { public: A(int a=1,...
5
by: john sun | last post by:
Hi, I have a class with copy ctor and assignment operator. MyClasss obj1(data); MyClass obj2=obj1; in this case, the copy constructor is invoked instead of assignment operator. I cannot...
50
by: Charles Stapleton | last post by:
Given the folowing class class Ctest{ public: Ctest( int i, int j) :a(i) { b = j; } private: int a, b; } When creating an object of type Ctest, what advantage is there to setting
17
by: Thomas Lorenz | last post by:
Hello, I'm a little confused about object initialization. According to Stroustrup (The C++ Programming Language, Special Edition, Section 10.2.3) constructor arguments should be supplied in one...
5
by: Grahamo | last post by:
Hi, I have a basic question regarding some legacy code I'm working with; Basically the code looks something like this. I'd like to know if there are any reasons why a particular approach is...
5
by: heng | last post by:
class A { public: int x; A(int x_=0):x(x_){} }; int main() { A obj1(99); //user-defined constructor is used
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
16
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; class Test { static Test t; static Test init_Test( ) { return t; }
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.