Connecting Tech Pros Worldwide Help | Site Map

question about "operator ="

  #1  
Old September 29th, 2006, 03:15 AM
tomy
Guest
 
Posts: 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

  #2  
Old September 29th, 2006, 03:25 AM
Victor Bazarov
Guest
 
Posts: n/a

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


  #3  
Old September 29th, 2006, 04:15 AM
tomy
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
question about "++" mdh answers 25 June 2nd, 2007 01:45 PM
overload "operator->" Jess answers 14 May 6th, 2007 08:15 PM
Use of the "new" operator in widget toolkits sven_c_t@hotmail.com answers 1 July 23rd, 2005 07:23 AM
Give * operator "deep copy" Nick Jacobson answers 2 July 18th, 2005 03:47 PM