<p>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.</p>

<p>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.</p>

[{'answer_id': 77832, 'author': 'Sebastian Redl', 'author_id': 8922, 'author_profile': 'https://Stackoverflow.com/users/8922', 'pm_score': 0, 'selected': False, 'text': '<p>If every class has its own dynamic library, just check if the library exists.</p>\n'}, {'answer_id': 77841, 'author': 'Jason Dagit', 'author_id': 5113, 'author_profile': 'https://Stackoverflow.com/users/5113', 'pm_score': 0, 'selected': False, 'text': '<p>This sounds like a place to use "compile time polymorphism" or template policy parameters.</p>\n\n<p>See Modern C++ Design by Andrei Alexandrescu and his <a href="http://sourceforge.net/projects/loki-lib/" rel="nofollow noreferrer">Loki</a> implementation based on the book.  See also the <a href="http://en.wikipedia.org/wiki/Loki_(C%2B%2B)" rel="nofollow noreferrer">Loki</a> page at wikipedia.</p>\n'}, {'answer_id': 77843, 'author': 'Daniel Spiewak', 'author_id': 9815, 'author_profile': 'https://Stackoverflow.com/users/9815', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n\n<p>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 <em>new</em> constant which contains the class name which is being compiled.  Then, the names are still hard coded, but all in a central location.</p>\n"}, {'answer_id': 77851, 'author': 'user10392', 'author_id': 10392, 'author_profile': 'https://Stackoverflow.com/users/10392', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 77856, 'author': 'Dima', 'author_id': 13313, 'author_profile': 'https://Stackoverflow.com/users/13313', 'pm_score': 1, 'selected': False, 'text': '<p>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. </p>\n'}, {'answer_id': 77911, 'author': 'Denice', 'author_id': 14178, 'author_profile': 'https://Stackoverflow.com/users/14178', 'pm_score': 3, 'selected': True, 'text': '<p>Are you looking for C++ runtime class registration? I found this <a href="http://meat.net/2006/03/cpp-runtime-class-registration/" rel="nofollow noreferrer">link</a> (<a href="http://web.archive.org/web/20100618122920/http://meat.net/2006/03/cpp-runtime-class-registration/" rel="nofollow noreferrer">backup</a>).</p>\n\n<p>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.</p>\n'}, {'answer_id': 77935, 'author': 'David Thornley', 'author_id': 14148, 'author_profile': 'https://Stackoverflow.com/users/14148', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>If you could specify classes at run-time based on their name, something like:</p>\n\n<p>std::string foo = "Derived1";\nBase * object = new "foo";   // or whatever notation you like - doesn\'t work in C++</p>\n\n<p>then the ability to tell if "Derived1" was compiled or not would be useful.  Since you have to specify the class directly, like:</p>\n\n<p>Base * object = new Derived1;  // does work in C++</p>\n\n<p>all checking is done at compile time.</p>\n'}]
['https://Stackoverflow.com/questions/77817', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13986/']