Let's suppose I have a shared library named libplugin
. In this shared library, there is a class:
class Plugin
{
public:
virtual void doStuff();
};
Let's also suppose that there is another shared library named libspecialplugin
. It contains the following class and function:
class SpecialPlugin : public Plugin
{
public:
virtual void doStuff();
};
Plugin *createSpecialPlugin()
{
return new SpecialPlugin;
}
Now, suppose I change Plugin
and add the following method:
virtual void doMoreStuff();
I do not recompile libspecialplugin
.
What happens when I do this:
Plugin *plugin = createSpecialPlugin();
plugin->doMoreStuff();
I'm guessing one of the following happens:
Plugin::doMoreStuff()
method is invokedDoes the libspecialplugin
library contain information that libplugin
can use to determine which of its methods are overridden - even at runtime? I'm a little fuzzy on what exactly is supposed to happen here.
You are effectively violating the "One Definition Rule" by having the same class (Plugin
) defined differently in two different translation units within any program that uses the two libraries.
The standard says (C++11 ISO 14882:2011, §3.2 para 5):
There can be more than one definition of a class type (Clause 9) ... in a program provided that each definition appears in a different translation unit, and provided the definitions satisfy the following requirements. Given such an entity named D defined in more than one translation unit, then:
- each definition of D shall consist of the same sequence of tokens; and
...
Your class Plugin
has two different definitions, one baked into libplugin and the other in libspecialplugin, so it does not comply with the standard.
The outcome of this is not defined by the standard, so anything could happen.