c++inheritancec++17aggregate

When an aggregate inherits from an aggregate, is there risk of object slicing or memory leaks?


I am reading some material about C++17, I have a question about inheritance between aggregates.

If an aggregate inherits from an other aggregate, isn't it a problem ? Is there not any risk of slicing and memory leak ? I thought that when there were as inheritance, there should be virtual destructor.


Solution

  • If an aggregate inherits from an other aggregate, isn't it a problem ?

    No, not in itself. It all depends on how you use it.

    Is there not any risk of slicing

    I wouldn't see it as a risk. You can pass an object created from a derived class to a function accepting a reference to the base class and it can then operate on the base class members.

    and memory leak ?

    The object of the derived class will destroy its members when it goes out of scope, so not really.

    I thought that when there were as inheritance, there should be virtual destructor.

    Only if you aim to destroy an object of a derived class through a base class pointer do you really need a virtual destructor.