0

Hi I'm trying to fully understand inheritance before I start using abstract classes / virtual methods.

My question is: Is there a way of re-using a function (in this case an operator overload) of the base class, which return a base class object, in the derived class?

The example is the following: Reptile is a public derived class of Creature. Creatures have a Position object (x,y) as member data. When I add a creature and a position, the new creature has the old position summed to the added position.

Creature Creature::operator+(const Position& source) const
{
  Creature copy{*this};
  return copy += source; // += has the specific implementation of the summation
}

Main:

Position pos{};
Reptile rep{};
Reptile rep2 = rep + pos; // ERROR: No viable conversion from 'Creature' to 'Reptile'

I understand why it is not working, technically, but isn't the whole point of inheritance that I can reuse functionalities of the base class, specially when it's dealing with variables defined in the base class?

Thank you

Michel H
  • 317
  • 3
  • 10
  • 1
    Does this answer your question? [How to use base class's constructors and assignment operator in C++?](https://stackoverflow.com/questions/1226634/how-to-use-base-classs-constructors-and-assignment-operator-in-c) – Dean Johnson Mar 23 '21 at 15:48
  • 2
    You're just asking the same question over and over again: https://stackoverflow.com/a/66700930/6119582 still applies. – sweenish Mar 23 '21 at 15:49
  • 3
    _"isn't the whole point of inheritance that I can reuse functionalities of the base class"_ -- be **very** careful about this mindset. There are so many ways to reuse functionality without having to resort to a base-class -- and using base classes just for code reuse often leads to horrible god-class architectures. Inheritance is about modeling hierarchical relationships -- that's it. A *property* of inheritance is code reuse, but not *justification* for it – Human-Compiler Mar 23 '21 at 15:50
  • I disaggree sweenish, this question is not specific to the overload operator like in the other question. I just used it as a representative example of a base class method. The question here is about calling base class methods directly by instantiated derived class objects, and not calling base class methods from within the derived class method (which was my other question). – Michel H Mar 23 '21 at 15:54
  • Dean Johnson, did you close the question? It is NOT the same one as the one you just tagged, since I explicitly stated that I DON'T want to use virtual functions. It is a question around understanding the code under the given constraints, and not optimising the code. – Michel H Mar 23 '21 at 15:58
  • While a `Reptile` *is a* `Creature` when you consider the inheritance relationship, you didn't define a way to add a `Reptile` and a position. So the compiler complains. All you would do is tell the `Reptile` function to call the base class operator. As my previous answer demonstrates. Otherwise Human-Compiler's comment is the mindset you need to adopt. – sweenish Mar 23 '21 at 16:02
  • I understand that and it is what I did originally. My question is more of an open nature, WHY I have to redefine it, or if there's a more elegant way of leveraging the base class method without redefining it in the derived class. That is why I wrote: **"I understand why it is not working, technically, but isn't the whole point of inheritance that I can reuse functionalities of the base class, specially when it's dealing with variables defined in the base class?"** – Michel H Mar 23 '21 at 16:08
  • It seems like there's no other option, if I'm the returning objects are inherently different. But thanks anyways! – Michel H Mar 23 '21 at 16:11

0 Answers0