I have a class A and class B which both have the same methods and variables, but B has one additional variable (which is completely independent from other class members). So it would be something like:
class A
{
void Foo();
bool m_var;
}
template< class T >
class B< T >
{
// Same stuff
void Foo();
bool m_var;
// Unique stuff
T m_data;
}
Normally, I would use inheritance B : public A
, but I want to keep these classes super tight and I don't want to have vtable ptr inside them (as I'm not gonna use polymorphy anyway). What's the best approach to achieve that? I was thinking about templates and their specialization - having class A<T>
and A<void>
, but I need to remove something, not add. Is there any smart template trick which I could use?
I was also thinking about creating base class (without virtual dtor) with all common functionalities as a private nested class and inherited classes A< T > : public Base
and empty B : public Base
as public nested classes. It wouldn't allow anyone from outside to use base class ptr, but it doesn't sound like the purest solution... Is there any "valid" solution for my problem?