Beginner's question

P

Pierre Couderc

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
 
S

Srini

myClass a;

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
 
A

Alipha

Pierre said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top