Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

type similar to union

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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++

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
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++


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!