Below code instantiates a derived singleton object based on environment variable. The compiler errors saying error C2512: 'Dotted' : no appropriate default constructor. I don’t understand what the compiler is complaining about.
EDIT:
Fixed issues with implementing the get instance method which requires definition of both the parent and derived class. By separating the class definitions in separate header files and including the same in Singleton.cpp where the instance function is implemented.
Mainfile – 1
#include <iostream>
#include <string>
#include "Singleton.h"
using namespace std;
int main(){
Singleton::instant().print();
cin.get();
}
Singleton.h
#pragma once
#include <iostream>
using std::cout;
class Singleton{
public:
static Singleton & instant();
virtual void print(){cout<<"Singleton";}
protected:
Singleton(){};
private:
static Singleton * instance_;
Singleton(const Singleton & );
void operator=(const Singleton & );
};
Singleton.cpp
#include "Singleton.h"
#include "Dotted.h"
Singleton * Singleton::instance_ = 0;
Singleton & Singleton::instant(){
if (!instance_)
{
char * style = getenv("STYLE");
if (style){
if (strcmp(style,"dotted")==0)
{
instance_ = new Dotted();
return *instance_;
} else{
instance_ = new Singleton();
return *instance_;
}
}
else{
instance_ = new Singleton();
return *instance_;
}
}
return *instance_;
}
Dotted.h
#pragma once
class Dotted;
class Dotted:public Singleton{
public:
friend class Singleton;
void print(){cout<<"Dotted";}
private:
Dotted(){};
};
There are several problems with your code:
You mean to return typeSingleton&orconst Singleton&. You are currently returning by value, which is attempting to invoke a copy constructor, and no such constructor exists.Your default constructor in Dotted probably is not available in Singleton. I suggest you make Singleton a friend of Dotted so that it is able to access that constructor. Although not 100% sure on this one.You forgot to make the function print() virtual, so your override won’t manifest itself.Singleton* Singleton::instance_ = 0;in a source file somewhere.if(!style)section; currently, if the STYLE environment variable is set, but isn’t set to “dotted”, then you end up returning a null singleton.In addition to the above, I strongly advise you to avoid environment variables and singletons. Both of them are examples of “shared mutable state” and can lead to a whole lot of messiness. Singletons, though they have appeared in “design pattern” books for quite some time, are now being understood to be design anti-patterns. It is a much more flexible approach to have an interface that you pass around and simply happen to instantiate once rather than bake in the fact that it exists once into its API.
For example, for your particular case, I would suggest something like the following:
Then, in any class where you previously used Singleton, simply take a const Printer& object. And print to that object. Elsewhere, you can conditionally construct a StringPrinter(“Singleton”) or StringPrinter(“dotted”). Or possibly some other instance of that interface, although I would suggest using QSettings or some sort of configuration file in place of environment variables, or at least use MYAPPLICATIONNAME_STYLE instead of just STYLE; in other words, if you are going to go the environment variable route, at least qualify its name.