DarthRubik
DarthRubik

Reputation: 3985

Virtual/Non-virtual inheritance

Suppose I have a base class:

struct A{
  virtual void foo() = 0;  
};

And then suppose I have a derived class like this:

struct B : public virtual A{ 
  void foo(){ /*something*/ }
};

But I also want a copy of B like this (note that it is identical in every way except for not inheriting virtually):

struct B : public A{ 
  void foo(){ /*same something*/ }
};

Is there a some sort of template sorcery to do this with just one definition.

Something like this??

struct B : public InheritanceHelper<A> {
  void foo(){ /*same something*/ }
};

Upvotes: 1

Views: 72

Answers (1)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145439

I'm sorry but I don't see the real world problem you're trying to solve. That means that I can't give advice on how to solve that in a better way, but only a technical solution to the stated technical question. Using a technical solution like this can be counter-productive.

Still, a technical solution can go like this:

struct A
{
    virtual void foo() = 0;
};

struct Virtual: virtual A {};
struct Non_virtual: A {};

template< class Base >
struct B_: Base
{
    void foo() override {};    // Something.
};

using B_virtual = B_<Virtual>;
using B_nonvirtual = B_<Non_virtual>;

Disclaimer: code above not reviewed by a compiler.

Upvotes: 1

Related Questions