3

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.

X-Istence
  • 16,324
  • 6
  • 57
  • 74

7 Answers7

3

Are you looking for C++ runtime class registration? I found this link (backup).

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.

Gluttton
  • 5,739
  • 3
  • 31
  • 58
Denice
  • 116
  • 1
  • 2
    The site seems to be down, but I found the page at: http://web.archive.org/web/20100618122920/http://meat.net/2006/03/cpp-runtime-class-registration/ – Daniel James Aug 29 '12 at 08:20
  • 1
    I don't think this is guaranteed to work by the standard, as a class in a translation unit only has to be constructed before code in that translation unit is run. So if you never directly call code in that file, it doesn't have to be constructed. Or it may be constructed too late to be registered. So it's possible that with a different compiler it won't work. This is a based on a distant memory of a similar technique not working, so it might not be entirely accurate. – Daniel James Aug 29 '12 at 08:32
2

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.

user10392
  • 1,384
  • 8
  • 12
1

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
  • 38,860
  • 14
  • 75
  • 115
0

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

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
0

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
  • 13,684
  • 8
  • 33
  • 56
0

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
  • 54,515
  • 14
  • 108
  • 120
0

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
  • 56,304
  • 9
  • 91
  • 158