• 10
• 9
• 13
• 10
• 18

# type similar to union

This topic is 3724 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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).

##### Share on other sites
I don't know the answer to your question, but maybe you could settle for something like the State pattern or the Strategy pattern?

##### Share on other sites
Would using inheritance work for you instead?

__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]

##### Share on other sites
Quote:
 Original post by NypyrenWould 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++

##### Share on other sites
Why?

It's pointless to talk about solution without knowing the problem it's trying to solve.

##### Share on other sites
Is the type X.U compile time constant?

[Edited by - King Mir on January 8, 2008 3:05:08 PM]

##### Share on other sites
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...

##### Share on other sites
Quote:
 Original post by King MirIs the type X.U compile time constant?

im unsure what you mean...

##### Share on other sites
Quote:
Original post by Dragon_Strike
Quote:
 Original post by NypyrenWould 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 AntheusWhy?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.

##### Share on other sites
Quote:
 Original post by Dragon_StrikeWhat 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.