By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Beginner's question

P: n/a
What is the difference between these 2 ways of writing :

myClass a;
a=b;

and
myClass a=b;
I wrongly thought they were identical (MSVC6).

Thank you in advance.

Pierre Couderc
Aug 4 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> myClass a;
a=b;

The first staement would perform default iniitalization by calling the
default constructor of myClass. The second statement would call the
assignment operator of myClass.
myClass a=b;
This would perform copy initialization. Copy initialization means the
object is initialized using the copy constructor, after first calling a
user-defined conversion if necessary.
Thank you in advance.

Pierre Couderc


Srini

Aug 4 '05 #2

P: n/a

Pierre Couderc wrote:
What is the difference between these 2 ways of writing :

myClass a;
a=b;

a is default constructed first, then the assignment operator is used to
assign b to a.
and
myClass a=b;
This is initialization, not assignment. This is identical to:

myClass a(b); // if b is the same type as a

or:

myClass a(myClass(b)); // if b is of different type

In either case, the copy constructor and possibly another constructor
are called and no assignment operator is used.
I wrongly thought they were identical (MSVC6).

Thank you in advance.

Pierre Couderc


Aug 4 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.