My apologies in advance if I'm not asking this in a right Stack Overflow forum. I'm trying to come up with a design which is easily expandable and also easy to use. So here is my problem:
In C++, I have two classes, called Class One and Class Two which both supports an operation called doSomething(T& data). T is a struct which for example has two members for Class One and has four members for Class Two. Since I want to enforce doSomething(T& data) into those classes, I have a base class called Base.
template <class T>
class Base
{
typedef T data_type;
virtual bool doSomething(data_type& data) const = 0;
};
struct OneData { int a; int b; };
class One : public Base<OneData>
{
bool doSomething(data_type& data) const { ... }
};
struct TwoData { int a; float b; int c; char d; };
class Two : public Base<TwoData>
{
bool doSomething(data_type& data) const { ... }
};
One big problem I have with this approach is, I cannot defined a Base type variable. I'm forced to use One or Two as type which forces me to defined 2 variables (what I really want is to create Base type variable and allocate memory or initialize it to One or Two based on the given T).
Furthermore, I was thinking to create class called OneTwo which defines doSomething(T& data) which basically hides the headache of which object to create from clients.
// This is just idea of what I want but don't know how??
template<class OneT, class TwoT>
struct OneTwo
{
bool doSomething(T& data) const // Here I don't know how to get to T
{
if (OneT::owns(data)) // call OneT::doSomething
else if (TwoT::owns(data)) // call TwoT::doSomething
else // Maybe exception, error, ...
}
};
I'm not sure even if I did right approach on class One and Two. Also, I tried to explain it as clear as possible but please ask if anyone need more information.
Please help me to understand on how to design this?
Thank you,
Aucun commentaire:
Enregistrer un commentaire