C
Christopher
I've seen various ways of doing this, but what I want is to be able to
take a base class pointer and know which derived class to cast to.
For example I am going to make a base light class, that has a position
and intensity.
A derived classes class may be a point light which also has a falloff
value.
The derived class has all the base class methods and data in common,
but has the additional falloff value get/set methods.
Now my rendering interface takes a base light class pointer, but the
body needs to know if it is a point light or not, so it can cast and
get that fall off value.
Ways I've seen this done:
1) dynamic cast and check for NULL;
Well, we could potentially be going through 10 dynamic casts or
more before we know what we have. I heard it was rather expensive to
do that.
2) Put a static const int identifier in every derived class and check
for each in a switch, then you know what to
cast to.
I don't know if this is really more efficient than 1. It also
requires keeping track of all these IDs and which are used already
when you make a new derived class.
3) Any other sugestions?
take a base class pointer and know which derived class to cast to.
For example I am going to make a base light class, that has a position
and intensity.
A derived classes class may be a point light which also has a falloff
value.
The derived class has all the base class methods and data in common,
but has the additional falloff value get/set methods.
Now my rendering interface takes a base light class pointer, but the
body needs to know if it is a point light or not, so it can cast and
get that fall off value.
Ways I've seen this done:
1) dynamic cast and check for NULL;
Well, we could potentially be going through 10 dynamic casts or
more before we know what we have. I heard it was rather expensive to
do that.
2) Put a static const int identifier in every derived class and check
for each in a switch, then you know what to
cast to.
I don't know if this is really more efficient than 1. It also
requires keeping track of all these IDs and which are used already
when you make a new derived class.
3) Any other sugestions?