Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 29th, 2006, 03:15 AM
tomy
Guest
 
Posts: n/a
Default question about "operator ="

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
Default 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
Default 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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles