Can you overload an operator in multiple classes?

Need to use 4 overloaded ops for final project

Page 1 of 1

1 Replies - 877 Views - Last Post: 24 April 2010 - 08:24 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Can you overload an operator in multiple classes?

Posted 24 April 2010 - 07:38 PM

Ok, My question is, can you overload an operator in two separate classes, so basically redefine them in each class based on the classes needs, something like:
class base { }
class bigBase : public base
{
  //...
  friend operator+=(bigBase &, double);
};

class smallBase : public base
{
//...
friend operator+=(smallBase &, double);
};


or is it something that should be done by declaring it virtual? And if having to declare virtual, how would you do this?

Is This A Good Question/Topic? 0
  • +

Replies To: Can you overload an operator in multiple classes?

#2 noclaf  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 26
  • View blog
  • Posts: 94
  • Joined: 18-April 10

Re: Can you overload an operator in multiple classes?

Posted 24 April 2010 - 08:24 PM

This is one of the concepts of inheritance. It can be done, but you can't use a virtual. You can't override a function that already exists with a virtual initializer. At best, you could declare a general behavior in the parent and let the child choose whether or not to override it.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1