Implementing an InheritsFrom function

I have been looking everywhere for this. I use Borland C++ Builder, and part of the functionality of the TObject class is the InheritsFrom function. I'd like to implement this for a series of classes I'm writing that are just basic c++ classes (not part of the VCL).

Is there an easy way in standard c++ to determine if one class inherits from another? I'd like to write a function that will be inherited from my base class so I only have to write it once. Something like this:

class Base
{

public:

bool InheritsFrom(String ClassName);

};

Yes and no. The above requires a lot of unmaintainable machinery. You basically have to build a mapping of strings (class names) to C++ types.

You have RTTI at your disposal, which allows you at runtime, to attempt a polymorphic downcast. (Bad upcasts fail at compile time)

You also have the boost type traits library which allows you to do type checking at compile time.




In Aspect Oriented Programming, this is called a Cross Cut. You may want to use that as the basis for a web search.

Before you begin, I'll say categorically that C++ does not support AOP, it's primarily available on Smalltalk derived languages (like Java and C#), but someone may have tried to meld the two somewhere.
Topic archived. No new replies allowed.