Skip to main content
gamedev.net
🔒 Locked

Dynamic variable creating

Started by TheSorcerer 3 replies 100+ views
Original Post
TheSorcerer
TheSorcerer
Is there a way to set the type of a variable during runtime? Somethink like:
  
UnkownType *TempVar;

if (...)
{
     TempVar = new UCHAR;
}
else if (...)
{
     TempVar = new USHORT;
}
else if (...)
{
     TempVar = new UINT;
}
  
elis-cool
elis-cool
void my friend. The answer is void.

  
void* pUnknownType;
// your code;


or you could use a union, and just have all its possible types in it...
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
civguy
civguy
No. You need to make an (abstract) base class that provides the operations you need from the type.

    
class SomeType {
virtual void hitMe() = 0;
virtual ~SomeType() {}
};

class Type1 : public SomeType {
virtual void hitMe() {...}
};

class Type2 : public SomeType {
virtual void hitMe() {...}
};

...

SomeType *TempVar;

if (...)
{
TempVar = new Type1;
}
else if (...)
{
TempVar = new Type2;
}


[edited by - civguy on December 29, 2002 6:28:55 AM]
Evil Bill
Evil Bill
What are the types you need to cast to? In your example, why not just stick to UINT?

Member of the Unban Mindwipe Society (UMWS)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.