J
joer3
When you initialize a variable like this:
X var;
var = X();
The equals sign is called the assignment operator and you can overload
it and indeed should in many cases.
However, when you write code like this:
X var = X();
What is the equals sign considered? Is it still an operator? Can you
overload it? I can't imagine any reason why you would want to overload
it, just curious.
Also, it is my current understanding this is what happens under the
hood in these cases. Could someone confirm I understand correctly?
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".
4) Delete temp data from second call to default constructor (not sure
when that happens).
Second Case:
1) Allocate memory for an "X" variable.
2) Call default constructor for "X" and put it in var.
Thanks,
Joe
X var;
var = X();
The equals sign is called the assignment operator and you can overload
it and indeed should in many cases.
However, when you write code like this:
X var = X();
What is the equals sign considered? Is it still an operator? Can you
overload it? I can't imagine any reason why you would want to overload
it, just curious.
Also, it is my current understanding this is what happens under the
hood in these cases. Could someone confirm I understand correctly?
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".
4) Delete temp data from second call to default constructor (not sure
when that happens).
Second Case:
1) Allocate memory for an "X" variable.
2) Call default constructor for "X" and put it in var.
Thanks,
Joe