4

Is it possible to overload = operator without fully reimplementing it?

I want to specify special behavior for it - if input object has some special value -> operator should do some additional work. If not - it should work as basic assignment operator.

Something like

operator=(input)
    if (input == specialValue)
        setParam(this->true)
    base()
lapots
  • 12,553
  • 32
  • 121
  • 242
  • 1
    As soon as you overload it, the basic assignment operator is gone. – Nishant Jul 22 '15 at 06:28
  • Ok. Then the next question is - is possible to overload it only in base class, without need to implement it everywhere in the inherited class? – lapots Jul 22 '15 at 06:32
  • Give a default implementation in the base class, and an overload implementation in the derived class. – acraig5075 Jul 22 '15 at 06:36
  • Not quite sure, what you mean. Overloading depebds on the type and of course all overloads of a member function are inherited by derived types. – MikeMB Jul 22 '15 at 06:41
  • You need to have assignment operator in derived class, if you don't have it, then the default assignment operator will be provided, which in turn will call your base class's assignment operator. These links can help you http://stackoverflow.com/questions/3882186/trouble-with-inheritance-of-operator-in-c http://stackoverflow.com/questions/9161512/assignment-operator-inheritance – Nishant Jul 22 '15 at 06:43
  • Oh! I see. So I need to implement assignment for every inherited class...Pfff – lapots Jul 22 '15 at 06:49

1 Answers1

0

You can do it by using if else statement, in the else part specify the basic functionality and in if or else if part specify your condition if condition is true return some specific values according to that values perform your operation.

operator=(input)
        if (input == specialValue)
            setParam(this->true)
        esle
           setParam(input)
Muhammad Nasir
  • 2,126
  • 4
  • 35
  • 63
  • He _doesn't_ wan't to reimplement the entire thing, though, which is what you are doing in the `else` clause. – hlt Jul 22 '15 at 06:43