overloading operators?

Started by
12 comments, last by Tevong 22 years, 6 months ago
As Gillies stated you only need a new constructor for the operator overloader function as follow:

  CVehicle(unsigned int c_weight, unsigned int c_capacity, float c_topspeed);CVehicle(unsigned int c_weight, unsigned int c_capacity, float c_topspeed) : weight(c_weight), capacity(c_capacity), topspeed(c_topspeed) { }  


And the code will work as it should.
But since you almost always want to initialize the values one way or the other I would remove the CVehicle() constructor and replace it with this one that use default values:

  CVehicle(unsigned int c_weight = 0, unsigned int c_capacity = 0, float c_topspeed = 0.0f);  



Edited by - errand on October 24, 2001 7:31:06 AM
Death is lifes way saying your fired.
Advertisement
you can make the binary + a member function, just only have one parameter. However it is considered better form to have it be a non member function and implement it in terms of +=. That way it lets implicit constuctors work on the left operand
quote:Original post by Anonymous Poster
you can make the binary + a member function, just only have one parameter.

I looked that up, and he''s right. Thanks!

quote:However it is considered better form to have it be a non member function and implement it in terms of +=. That way it lets implicit constuctors work on the left operand

I think it''s a bad idea to implement operator+ in terms of operator+=, because a + normally takes its lhs and rhs operands, adds them together and returns the sum without modifying either of them. If you''re speaking of using operator+= on the sum object, well isn''t that inefficient? Consider:
// using += on one of the operands:myclass &operator+(myclass &m1, myclass &m2){  m1 += m2;  return m1;}// this has the effect of making c = a + b set c to the sum, but// a to the sum as well.//// using += on the sum object:myclass &operator+(myclass &m1, myclass &m2){  myclass m;  m += m1;  m += m2;  return m;}// this is just plain stoopid.//// what I consider to be the best way for pseudo-arithmetic types:myclass &operator+(myclass &m1, myclass &m2){  mylcass m;  m = m1 + m2; // this would actually be a + on the individual fields,               // such as the coordinates in a vector  return m;}// c = a + b behaves properly. 

Alternatively, as a member function:
myclass &myclass::operator+(myclass &m){  myclass r = *this;  r += m;  return r;  // 2 assignments, an extra copy of r and one addition.  // compare to one copy of the sum, one addition and one assignment for friend version} 
well that''s what I''ve read Stroustrup''s book and he seems like a really smart guy (plus he made the language) so I''m doing it his way. I think it was something like this

A operator+(A l, A r)
{
return l+=r;
}

or maybe this

A operator+(A& l, A& r)
{
A a = l;
return l+=r;
}

I think it was the first one so that implicit constructors would work. I''m not sure but I don''t think they would work if you use a reference. So if you have a type B and a constructor A(B) you could do b+a with the first one. If it were a member function you would have to always do a+b.

Another reason so implementing + in terms of += is that they always stay the same. You only need to change += and they both change. Also since += is a member function it will have access to private members while binary+ won''t (though of course you can almost always make it a friend). If += is inlined into + it should be about the same speed anyway I think.

This topic is closed to new replies.

Advertisement