type similar to union
i need some kind of type that is like "union" that can store classes and when called returns the actual type that is currently set...
for example
class Base
{
union xtype
{
class1 A;
class1 B;
class1 C;
}
xtype U;
void class_function(class1 a){}
void class_function(class2 b){}
void class_function(class3 c){}
}
main()
{
Base X;
X.U = class1(somevalue);
Base.class_function(X.U); // call the apropriate function
}
how can i do this? ive looked at boost::variant but i dont think it allows me to call function inside the same class as the variable... since i have to make a "visitor" class which cant access variables in the class (which i need to).
I don't know the answer to your question, but maybe you could settle for something like the State pattern or the Strategy pattern?
Would using inheritance work for you instead?
main()
{
A a1;
Base *base = &a1
base->class_function();
}
[Edited by - Nypyren on January 8, 2008 3:45:05 PM]
__declspec(novtable) class Base{ virtual void class_function() = 0;};class A : public virtual Base //, other base classes{ virtual void class_function() {}};class B : public virtual Base //, etc{ virtual void class_function() {}};
main()
{
A a1;
Base *base = &a1
base->class_function();
}
[Edited by - Nypyren on January 8, 2008 3:45:05 PM]
Quote:Original post by Nypyren
Would using inheritance work for you instead?
im sorry i forgot to mention the regular types should also be used... i could make wrappers for all the types that i need though... but it seems like a quite ugly solution.. or?
EDIT:: Im using C++
heh... well i guess soo... seems like i have a habit of simplifying my questions as much as possible...
i have a class which should store alot of objects/variables with different types and have "Set" functions for each type...
one way i could do this is to create on std::list<> for each type... but its a bit unpractical and ugly way to do it... also a bit slow where i have to decide which list to put new variables in...
what id like is a general type soo i can put all of them in the same list...
one way ive tried is to use boost::any... which works great except when i have to use "Set"... i have to first check which type it is than convert it to that type and call the appropriate "Set" function... which is kinda slow...
i have a class which should store alot of objects/variables with different types and have "Set" functions for each type...
one way i could do this is to create on std::list<> for each type... but its a bit unpractical and ugly way to do it... also a bit slow where i have to decide which list to put new variables in...
what id like is a general type soo i can put all of them in the same list...
one way ive tried is to use boost::any... which works great except when i have to use "Set"... i have to first check which type it is than convert it to that type and call the appropriate "Set" function... which is kinda slow...
Quote:Original post by King Mir
Is the type X.U compile time constant?
im unsure what you mean...
Quote:Original post by Dragon_StrikeQuote:Original post by Nypyren
Would using inheritance work for you instead?
im sorry i forgot to mention the regular types should also be used... i could make wrappers for all the types that i need though... but it seems like a quite ugly solution.. or?
EDIT:: Im using C++
You'll have to make a separate class_function for every single type anyway, so making wrappers isn't THAT ugly.
Quote:Original post by Antheus
Why?
It's pointless to talk about solution without knowing the problem it's trying to solve.
Actually, talking about it helps a person understand available options so that they can make better decisions for totally different problems in the future.
Quote:Original post by Dragon_Strike
What I'd like is a general type so I can put all of them in the same list...
This is precisely the case where you'd want to use inheritance.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement