Connecting Tech Pros Worldwide Forums | Help | Site Map

question about "operator ="

tomy
Guest
 
Posts: n/a
#1: Sep 29 '06
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


Victor Bazarov
Guest
 
Posts: n/a
#2: Sep 29 '06

re: question about "operator ="


tomy wrote:
Quote:
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, '='.
Quote:
The Obj b will be constructed by "operator =" directly,
No, it will be constructed by copy-constructor directly.
Quote:
or first by "Obj()" , then turn to "operator =", two steps.
No.
Quote:
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


tomy
Guest
 
Posts: n/a
#3: Sep 29 '06

re: question about "operator ="



Victor Bazarov wrote:
Quote:
tomy wrote:
Quote:
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, '='.
>
Quote:
The Obj b will be constructed by "operator =" directly,
>
No, it will be constructed by copy-constructor directly.
>
Quote:
or first by "Obj()" , then turn to "operator =", two steps.
>
No.
>
Quote:
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.
Quote:
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Closed Thread