Jump to content
  • Advertisement
Sign in to follow this  
supagu

auto increment variable

This topic is 4091 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

Okay i've seen a post like this before but i forgot about it, and now i need it :P what im doing is my own RTTI info in c++ i've been doing: class Entity { enum { Type = -1, }; virtual unsigned int GetType() { return Type; } } class SomeClass : public Entity { enum { Type = 1, }; virtual unsigned int GetType() { return Type; } } I can then go: if (unknownEntity->GetType() == SomeClass::Type) now, the problem is, i need to make sure each class is given a unique number for the type. This is pretty annoying to do it manually, so i was wondering if there was a preprocessor command/macro to do this. im using vc2005, but would be nice if it was a feature that would work with all standard c++ compilers

Share this post


Link to post
Share on other sites
Advertisement
Nope, there isn't. C++ uses independent translation units, which means you can't achieve something like this at all before link-time.

Share this post


Link to post
Share on other sites
In normal code there's no reason to use anything other than the RTTI method. If performance is crucial, then there is a large overhead in using RTTI for dynamic types, the comparison alone uses strcmp plus jumping into library code to find the type_info pointer, which does nasty things to the instruction cache. Also, this isn't possible using RTTI:

switch (object->Type)
{
case TypeA:
break;
case TypeB:
break;
//etc...
}

but that is just bad design because you can define an extra base class common to TypeA and TypeB to do the above:

object->DoSwitch ();

which will be more efficent.

But if you really want to automate this, try:

// a hash function, this isn't a great one
#define HASH(a) ((0[a])+(1[a]<<2)+(2[a]<<4)+(3[a]<<6)+(4[a]<<2)+(5[a]<<4)+(6[a]<<6)+(7[a]<<2)+(8[a]<<4)+\
(9[a]<<6)+(10[a]<<2)+(11[a]<<4)+(12[a]<<6))
#define CONCAT2(a,b) #a##b
#define CONCAT(a,b) CONCAT2(a,b)

class A
{
public:
virtual unsigned Type () { return TypeID; }
static const unsigned TypeID;
};

const unsigned A::TypeID = HASH(CONCAT(__LINE__,__FILE__));

class B : public A
{
public:
virtual unsigned Type () { return TypeID; }
static const unsigned TypeID;
};

const unsigned B::TypeID = HASH(CONCAT(__LINE__,__FILE__));

Skizz

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Is there any reason you aren't using C++'s RTTI system (in this case typeid and type_infos)?


problem is, there are situations when i need to get the super classes info from the child

eg:

class Shape
{
};

class Rectangle : public Shape
{
};

if (typeid(Shape) == typeid(Rectangle))
{
}

pretty much i want to say
"is rectangle a shape?"

without having to do a dynamic_cast as dynamic_casts are slow

i've come up with another system which doesnt use numbers, instead uses static variables and compares them by pointer

[Edited by - supagu on September 26, 2007 6:02:35 AM]

Share this post


Link to post
Share on other sites
I fail to see how your type number scheme would allow you to determine if an object is an instance of a base class.

Share this post


Link to post
Share on other sites
Perhaps you should try to detail the problem that is making you look at this custom typeid solution in order to get some more helpful responses. I think simple polymorphism will solve a lot of your problems.

I can only think of one instance in all the projects I've worked on where a typeid system was necessary and that was for a game that used 'composite entities', i.e. game entities were defined at run time as a collection of objects. As an example, a missile would be defined as having a position and direction object, an update object (could be a script) and a static graphic object. This meant developing a system to be able to determine which object in an entity implemented a given function (i.e. which one provides position information?).

Skizz

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!