I haven't tried putting that correction together with your original
code, but I think I understand the major problem in doing so.
You have made Singleton a templated class, so each compilation unit has
a different instantiation of Singleton, but NonSingleton is not a
templated class. You have included the definition of that one class in
multiple compilation units and then linked them together, so your code
is incorrect unless the class definitions of NonSingleton in those
multiple compilation units are the same.
You have made it so those class definitions have identical source code
but do not compile into identical class definitions.
That is just as much an error as if you had included different source
code for defining the same class.
Stefan Lampe wrote:
There's still a problem with the template parameters. Let me give the relevant class one last try with escapes:
template<typename ObjectT=Dummy>
class Singleton {
static ObjectT* object;
public:
static ObjectT& Instance() {
if (!object)
object = new ObjectT();
return *object; } };
template<typename ObjectT>
ObjectT* Singleton::object = 0;