Determining an instance of a class
OK, here''s a thought I''ve had recently, and something I''ve started working on.
What I''m doing is trying to build a ''quick and cheap'' Class idenification method. No RTTI, no typeid''s ... just a couple of constant strings across instances of classes.
For example, I define a base ''object'' class as such:
class Object
{
public:
Object();
virtual ~Object();
virtual const char * ClassName() = 0;
};
The ''ClassName'' method is pure virtual. This means that every class derived from Object has to instantiate it ... like so:
class Point : public Object
{
public:
Point();
virtual ~Point();
// **REALLY** simple RTTI information
const char* ClassName() {return "Point";}
}
At any point, I can get the ClassName, and do whatever comparisons I need to to determine if I''m looking at the right instance of the class.
I could also add in Object a series of methods for determining ''isa'' and ''isderivedfrom''.
What''s wrong with this? Remember, I just want a *simple* version of RTTI.
Humble,
Yet l33t
quote:What''s wrong with this? Remember, I just want a *simple* version of RTTI.
Should there be something wrong with it?
3DS Max uses a very similar idea to identify plugin types.
You may want to avoid high frequency use of string comparisons for performance reasons (using say an integer "identifier" instead).
--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
*Should* there be anything wrong with it?
Personally, I don''t think so, but I''m just wondering if there would be any *better* method of doing it.
Yeah, the string compares would *have* to be set to a minimal amount.
Humble,
Yet l33t
Personally, I don''t think so, but I''m just wondering if there would be any *better* method of doing it.
Yeah, the string compares would *have* to be set to a minimal amount.
Humble,
Yet l33t
This method would not work with inheritence. If I had class Animal, and class Dog : public Animal, the test to see wether a Dog object is-a Animal would fail. You would need to keep some kind of static lists of inheritence and check all the way up the list to solve this.
String compares would be slow. An enum or integer compare would be faster, but difficult to maintain--all classes would make sure they have unique numbers. You would have to find some way to make the class name itself part of an identifier.
Of course, built-in RTTI already solved these problems, but it''s probably just not |33+ enough for you.
String compares would be slow. An enum or integer compare would be faster, but difficult to maintain--all classes would make sure they have unique numbers. You would have to find some way to make the class name itself part of an identifier.
Of course, built-in RTTI already solved these problems, but it''s probably just not |33+ enough for you.
quote:Original post by Stoffel
This method would not work with inheritence. If I had class Animal, and class Dog : public Animal, the test to see wether a Dog object is-a Animal would fail. You would need to keep some kind of static lists of inheritence and check all the way up the list to solve this.
String compares would be slow. An enum or integer compare would be faster, but difficult to maintain--all classes would make sure they have unique numbers. You would have to find some way to make the class name itself part of an identifier.
Of course, built-in RTTI already solved these problems, but it's probably just not |33+ enough for you.
Again, all I'm looking for with this is a *minimal* set of tests. Not concerned at this point too much about an 'isa' test that unwinds the hierarchy stack.
Essentially, what I'm creating is a simple scene graph manager, and I want to be able to determine if a class is a specific instance of a class, when I want to access specific features of a class.
ie: I've got a SceneObject which defines the base functionality for the entire scene graph. However, specific instances of a class (Textured Polygon, for instance) have accessors for setting texture information. I *may* want to expose that functionality to the game component so that I can change a texture at run-time (via a console). This would give me a simple method for upcasting the SceneObject (stored in a list) to a TexturedPoly, and access it's methods.
Yeah, RTTI gives me that and so much more, but this is all about trying something different. I could build a more *robust* system that tracks everything, but I'm only interested right now in determining what the name of the instance of an object really is.
I could add RTTI but what would I learn (other than RTTI) from that? I'm just trying to explore some options here. After I get this built, I'll build a second application and try it with the compiler specific RTTI (Visual C++), and see what the code bloat/performance hit would be, if any ...
Remember, all I'm looking for is a *really* small subset of RTTI. I'm just curious as to what the differences will be performance wise between the two. Just something I'm doing more for curiosity sake than anything else.
Humble,
Yet l33t
Edited by - ashleymatheson on September 4, 2001 1:42:30 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement