question about "operator ="

T

tomy

Hi All:
I just wonder what happened when i use operator "=" to construct an
obj, as following:

class Obj
{
public:
Obj();
Obj& operator = (const Obj &);
}
Obj a;
Obj b = a;

The Obj b will be constructed by "operator =" directly,
or first by "Obj()" , then turn to "operator =", two steps.

I've write some "cout" in both of the functions, with gcc, no print any
more;
But finally the Obj b contain the same value with Obj a, obviously;

So what happend?

Thanks
 
V

Victor Bazarov

tomy said:
Hi All:
I just wonder what happened when i use operator "=" to construct an
obj, as following:

class Obj
{
public:
Obj();
Obj& operator = (const Obj &);
}
Obj a;
Obj b = a;

This syntax is called copy-initialisation, it has nothing to do
with assignment except using the same token, '='.
The Obj b will be constructed by "operator =" directly,

No, it will be constructed by copy-constructor directly.
or first by "Obj()" , then turn to "operator =", two steps.
No.

I've write some "cout" in both of the functions, with gcc, no print
any more;
But finally the Obj b contain the same value with Obj a, obviously;

So what happend?

Copy-initialisation. Isn't that what your favourite C++ book says?

V
 
T

tomy

Victor said:
This syntax is called copy-initialisation, it has nothing to do
with assignment except using the same token, '='.


No, it will be constructed by copy-constructor directly.


Copy-initialisation. Isn't that what your favourite C++ book says? Wow~~~, That's it.

V
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top