1

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*/ }
};
DarthRubik
  • 3,927
  • 1
  • 18
  • 54

1 Answers1

1

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.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331