[comp.lang.c++] Bear with me, my stupid question

grimlok@hubcap.clemson.edu (Mike Percy) (07/27/90)

If I've declared something as
 
class X {
   int count; 
public:
   X(X& another_X);   // construct a new X from an old X
   X operator +(X& rhs);
   :
} 

and I have
X X::operator +(X& rhs)
{
  // make a temporary copy of the lhs or rhs, withever is larger 
  X temp(count > rhs.count ? this : rhs);
  // do stuff with temp
  return temp;
}
 
This causes problems as this is of type
X *this 
and rhs is of type
X& rhs
 
I don't want to write it as X X::operator +(X *rhs) because I want to do
stuff like
X a, b, c;
c = a + b;      // don't want to have to do c = a + &b;
 
How do I properly get a X& from X *this? 

steve@taumet.com (Stephen Clamage) (07/28/90)

grimlok@hubcap.clemson.edu (Mike Percy) writes:

|X X::operator +(X& rhs)
|{
|  // make a temporary copy of the lhs or rhs, withever is larger 
|  X temp(count > rhs.count ? this : rhs);
                              ^^^^
			      *this
|  // do stuff with temp
|  return temp;
|}

Use '*this' instead of 'this' where indicated.  An object of type T may
be freely used where one of type T& is expected, and vice versa.  Now
both sides of the ':' operator have equivalent types.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com