On Jul 22, 12:44 am, joer3 <joery...@gmail.comwrote:
Quote:
|
When you initialize a variable like this:
|
Quote:
The equals sign is called the assignment operator and you can
overload it and indeed should in many cases.
|
Quote:
|
However, when you write code like this:
|
Quote:
|
What is the equals sign considered?
|
Punctuation. In this case, it's not an operator, and can't be
overloaded.
Quote:
Is it still an operator? Can you overload it? I can't imagine
any reason why you would want to overload it, just curious.
|
No and no.
Quote:
Also, it is my current understanding this is what happens
under the hood in these cases. Could someone confirm I
understand correctly?
|
Quote:
First Case:
1) Allocate memory for an "X" variable.
2) Call default constructor for "X" and put that in "var" (no copy
right?).
3) Call default constructor of "X" again and copy it to "var".
|
And copy assign it to var. There's an important difference:
copy assignment works on a fully constructed variable.
Quote:
4) Delete temp data from second call to default constructor
(not sure when that happens).
|
The last two points would best be described as:
-- construct a temporary X using the default constructor,
-- copy assign it to var, using the assignment operator,
-- destruct the temporary.
Quote:
Second Case:
1) Allocate memory for an "X" variable.
2) Call default constructor for "X" and put it in var.
|
Formally:
-- construct a temporary X using the default constructor,
-- copy it into var, using the copy constructor,
-- destruct the temporary
The standard explicitly authorizes the compiler to optimize out
the copy, however (and all that I know of do).
Your description would correspond to:
X var ;
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34