M
Martin
Greetings
I want to have virtual member functionality, but without my member
functions
being virtual
As of yet this is all just in my head cause I can't see a nice
solution yet so lets start with some simple code.
template <class T>
class Base
{
public:
void Func()
{
T* ptrT = static_cast<*T>(this);
pT->FuncImpl();
};
protected:
void FuncImpl() {std::cout << "This is Base"<<std::endl;}
};
class DerivedOne : public Base<DerivedOne>
{
public:
void FuncImpl() {std::cout <<"This is DerivedOne"<<std::endl; }
};
class DerivedTwo : public Base<DerivedTwo>
{
public:
void FuncImpl() {std::cout <<"This is DerivedTwo"<<std::endl; }
};
The thing I want to be able to todo is something like this
Base * b = GetSomeDerivedClass();
b->Func();
I'm quite sure this won't work as Base is a template class, but how
can I achieve something similar?
I recently read about someone wanting to store template classes of
different
types in an STL container, which is sort of similar to my problem and
one
of the suggestions was to use boost::any.
Is that the only suggestion if it is a solution at all?
Martin
I want to have virtual member functionality, but without my member
functions
being virtual
As of yet this is all just in my head cause I can't see a nice
solution yet so lets start with some simple code.
template <class T>
class Base
{
public:
void Func()
{
T* ptrT = static_cast<*T>(this);
pT->FuncImpl();
};
protected:
void FuncImpl() {std::cout << "This is Base"<<std::endl;}
};
class DerivedOne : public Base<DerivedOne>
{
public:
void FuncImpl() {std::cout <<"This is DerivedOne"<<std::endl; }
};
class DerivedTwo : public Base<DerivedTwo>
{
public:
void FuncImpl() {std::cout <<"This is DerivedTwo"<<std::endl; }
};
The thing I want to be able to todo is something like this
Base * b = GetSomeDerivedClass();
b->Func();
I'm quite sure this won't work as Base is a template class, but how
can I achieve something similar?
I recently read about someone wanting to store template classes of
different
types in an STL container, which is sort of similar to my problem and
one
of the suggestions was to use boost::any.
Is that the only suggestion if it is a solution at all?
Martin