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

question about "operator ="

P: n/a
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

Sep 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
tomy wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 29 '06 #2

P: n/a

Victor Bazarov wrote:
tomy wrote:
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?
Wow~~~, That's it.
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 29 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.