Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Overloading Operators Question....using default representation?

Status
Not open for further replies.

pghsteelers

Technical User
Apr 21, 2006
121
US
In learning C++ I have come across Overloading operators. At first I was thinking that it was strictly because you "can" manipulate the meaning for an operator so when it is used with a class you are actually calling a member function to perform some operation.

However, I have two question since learning about overloading operators:

1) what happens when you just use the operatore in your program expecting it to behave as the default representation? What happens when you want to use that operatore that you have overloaded with class objects and want to use it in as the default representation and not be calling a member function?

2) Is there any "must use" circumstances that you have to define a overload operator for a class or is it strictly for objective reasons?

Thanks in advance,
 
Well if you overload the operator+() to do subtraction instead of addition, that would just be stupid. So the first rule of overloading operators is to keep the meaning as close as possible to the default meaning.
If you overload operator+() to append the data in one object to the data in another object, that would be a better use of that operator.

I can't think of any situations in which you'd have to use operator overloading. It's mostly just for making things easier and look more natural. Most programming languages don't even support operator overloading.
 
Thanks for all the info:

Quote: Most programming languages don't even support operator overloading.

so only C++ supports this?
 
It's the only one I've seen that does; but there's hundreds of programming languages out there, so maybe there's others?
 
There are only two intrinsic operators for class objects: assignment (=, member by member) and address (unary &). You don't overload another operators, you define new ones. It's not possible to overload basic types operators.
See (may be it helps):
Code:
class X
{
  int	x;
public:
  X(int xx = 0):x(xx) {}
  X& operator =(const X& xx) 
  {
     if (this != &xx)
     {
        x = xx.x; 
        cout << "X::=" << endl; 
     }
     return *this; 
  }
  operator int() const { return x; }
};

class Y: public X
{
public:
  Y(int xx = 0):X(xx){}

  Y& operator =(const Y& yy) 
  {
     if (this != &yy)
     {
        cout << "Y::=";
        this->X::operator=(yy); 
     }
     return *this; 
  }
};

int main(int argc, char* argv[])
{
  X a(1939);
  X z(1945);
  cout << a << "-" << z << endl;
  X	t;
  t = a;
  Y s, q(2006);
  s = q;
  cout << s << endl;
  return 0;
}
No language rules to define overloaded operators in must use manner. As usually, you pragmatically must define a special assignment for classes with dynamically allocated members w/o proper destructors (or declare operator= as a private member to avoid these uncontrolled assignments).

It's a very dangerous (semantically incostintent) idea to force predefined op for class with redefined op.

Operator redefinition is in Fortran 2003. In Algol 68 (one of C and C++ ideological predecessors) you may invent new tokens for you own exotic operators...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top