Shep
Shep

Reputation: 8370

abstract derived class

My group has code with the following structure

class Base
{
public:
  float some_base_function(); 
};

class Derived : public Base
{
public: 
  float some_other_function(); 
  float yet_another_function(); 
}; 

which is simple enough. The issue is that I'm currently thinking about reimplementing Derived in a few experimental ways. My thinking was to do this:

class IDerived : public Base
{ 
public: 
  virtual float some_other_function() = 0; 
  virtual float yet_another_function() = 0; 
}; 

And then change the old Derived to inherit from IDerived. Is this sort of Concrete --> Abstract --> Concrete inheritance structure even allowed in C++?

To make matters worse, the original Derived class is persistified within the framework, so it must maintain the same structure in memory (which I hoped to achieve by making IDerived abstract). Will the new Derived have the same memory layout?

Upvotes: 2

Views: 2873

Answers (2)

Luchian Grigore
Luchian Grigore

Reputation: 258608

It's legal, but it doesn't seem like you need that. What's the point of the extra inheritance?

Moreover, Base and Derived don't have any virtual methods, so by adding any you can be 99% sure the memory layout won't be the same.

Upvotes: 3

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272497

Yes, that is allowed. But general practice is to avoid concrete base classes of any sort (see e.g. Item 33 of Scott Meyers' More Effective C++).

Attempting to persist complex objects by storing their binary representation is bound to lead to all sorts of problems; I would suggest finding a better mechanism (serialization, etc.).

Upvotes: 1

Related Questions