which is better and why???
-
consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)
-
consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)
The efficiency of the two does not realy differ very much. Of course, the first way comes with some additional machine instructions. It mainly depends on what you want to do with your constructor. William
-
consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)
namaskaaram wrote:
which one of the above is better??(in terms of efficiency)
I think first one is good to direct the reference correctly. As well we can give the gurantee of consistency in data. Knock out 't' from can't, You can if you think you can :cool:
-
consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)
Actually "copy constructor" is this one:
C(const C & )
; Regarding overloading assignment operators, I think your first variant, which returns a reference, allows multiple assignments like this:a = b = c
, or compact expressions like this:f(a = b)
. The second one does not allow this, but is little faster because of missing return value. -- modified at 4:24 Tuesday 20th June, 2006 -
Actually "copy constructor" is this one:
C(const C & )
; Regarding overloading assignment operators, I think your first variant, which returns a reference, allows multiple assignments like this:a = b = c
, or compact expressions like this:f(a = b)
. The second one does not allow this, but is little faster because of missing return value. -- modified at 4:24 Tuesday 20th June, 2006actually i meant assignment operator overloading....sorry abt the typo!:doh: ;o) thank u all!!!! :)
-
consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)
in fact, prefer the following operator :
C& C::operator = (const C&);
the reason to prefer this one instead of the void operator is that the user of your class will then be able to cascade the assignment operations. consider this :
int i1, i2, i3;
C c1, c2, c3;i1 = i2 = i3 = 5;
c1 = c2 = c3 = /*...*/;this is possible only because the operator = returns a reference to the object it has just modified...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
in fact, prefer the following operator :
C& C::operator = (const C&);
the reason to prefer this one instead of the void operator is that the user of your class will then be able to cascade the assignment operations. consider this :
int i1, i2, i3;
C c1, c2, c3;i1 = i2 = i3 = 5;
c1 = c2 = c3 = /*...*/;this is possible only because the operator = returns a reference to the object it has just modified...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
Yes, you're right. The rationale is that the assignment operator should behave in the same way as assignment for primitive types. Kevin
-
consider a class Class C { private: int a; int b; }; then i write a copy constructor. i believe copy constructor can be written in two wayz: C& C::operator=(const C&); void C::operator=(const C&); In the former case, we deference by returning this* and the latter we dont return anything, so here is my question: which one of the above is better??(in terms of efficiency)
namaskaaram wrote:
i believe copy constructor can be written in two wayz:
That is not a copy constructor, it's an assignment operator.
-
Actually "copy constructor" is this one:
C(const C & )
; Regarding overloading assignment operators, I think your first variant, which returns a reference, allows multiple assignments like this:a = b = c
, or compact expressions like this:f(a = b)
. The second one does not allow this, but is little faster because of missing return value. -- modified at 4:24 Tuesday 20th June, 2006Viorel. wrote:
The second one does not allow this, but is little faster because of missing return value.
You'll save 1, or maybe 2 CPU cycles. (Depending on how the compiler allocates registers, you may end up saving no cycles)