tags:

views:

1458

answers:

8

I have multiple classes that all derive from a base class, now some of the derived classes will not be compiled depending on the platform. I have a class that allows me to return an object of the base class, however now all the names of the derived classes have been hard coded.

Is there a way to determine what classes have been compiled, at run-time preferably, so that I can remove the linking and instead provide dynamically loadable libraries instead.

A: 

If every class has its own dynamic library, just check if the library exists.

Sebastian Redl
A: 

This sounds like a place to use "compile time polymorphism" or template policy parameters.

See Modern C++ Design by Andrei Alexandrescu and his Loki implementation based on the book. See also the Loki page at wikipedia.

Jason Dagit
A: 

There are nasty, compiler-specific tricks for getting at class information at runtime. Trust me, you don't want to open that can of worms.

It seems to me that the only serious way of doing this would be to use conditional compilation on each of the derived classes. Within the #ifdef block, define a new constant which contains the class name which is being compiled. Then, the names are still hard coded, but all in a central location.

Daniel Spiewak
+2  A: 

I don't know what you're really trying to accomplish, but you could put a singleton constructor in each derived class's implementation file that adds the name to a list, along with a pointer to a factory. Then the list is always up to date and can create all the compiled in classes.

+1  A: 

Generally, relying on the run-time type information is a bad idea in C++. What you have described seems like the factory pattern. You may want to consider creating a special factory subclass for each platform, which would only know about classes that exist on that platform.

Dima
+2  A: 

Are you looking for C++ runtime class registration? I found this link: http://meat.net/2006/03/cpp-runtime-class-registration/

That would probably accomplish what you want, I am not sure about the dynamically loaded modules and whether or not you can register them using the same method.

Denice
A: 

The names of the derived classes have to be hard-coded in C++. There's no other way to use them. Therefore, not only is there no way to automatically detect what classes have been compiled, there would be no way to use that information if it existed.

If you could specify classes at run-time based on their name, something like:

std::string foo = "Derived1"; Base * object = new "foo"; // or whatever notation you like - doesn't work in C++

then the ability to tell if "Derived1" was compiled or not would be useful. Since you have to specify the class directly, like:

Base * object = new Derived1; // does work in C++

all checking is done at compile time.

David Thornley
A: 

Denice, I will experiment and find out if it works with dynamically loaded libraries as well, that is not the exact solution I was looking for, but I can modify it to do what I need it to do.

X-Istence