Singleton with inheritance, Derived class is not able to get instantiated in parent?
Posted
by yesraaj
on Stack Overflow
See other posts from Stack Overflow
or by yesraaj
Published on 2010-05-09T10:52:32Z
Indexed on
2010/05/09
10:58 UTC
Read the original article
Hit count: 412
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.
#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
class Dotted;
class Singleton{
public:
static 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_;
}
}
return *instance_;
}
void print(){cout<<"Singleton";}
~Singleton(){};
protected:
Singleton(){};
private:
static Singleton * instance_;
Singleton(const Singleton & );
void operator=(const Singleton & );
};
class Dotted:public Singleton{
public:
void print(){cout<<"Dotted";}
protected:
Dotted();
};
Dotted::Dotted():Singleton(){}
int main(){
Singleton::instant().print();
cin.get();
}
© Stack Overflow or respective owner