开发者

Singleton with inheritance, Derived class is not able to get instantiated in parent?

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开发者_JAVA百科 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 type Singleton& or const 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.
  • You have put "friend" in the wrong place; you need to declare Singleton a friend of Dotted in Dotted, not within Singleton.
  • You should not make your definition of Singleton::instant inline as it needs to construct an instance of Dotted and, in order to do that, it needs to see Dotted's definition. So you should move that to a source file where it is able to see both Dotted and Singleton's complete definitions, respectively.
  • You need to put Singleton* Singleton::instance_ = 0; in a source file somewhere.
  • You are missing an else clause in your 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:

class Printer
{
    public:
        virtual ~Printer(){}
        virtual void print()const = 0
};

class StringPrinter : public Printer
{
    public:
         StringPrinter() : _str("") {}
         StringPrinter(const std::string& str) : _str(str) {}
         StringPrinter(const StringPrinter& o) : _str(o._str) {}
         virtual ~StringPrinter(){}
         virtual void print()const{ std::cout << _str << std::endl; }
         StringPrinter& operator=(const StringPrinter& o){ _str = o._str; return *this;}
    private:
         std::string _str;         
};

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.


It is not a great error message. The problem is that the compiler cannot generate code for the constructor call, it hasn't yet seen the definition of the Dotted class. C++ compilers are still single-pass compilers. You cannot write the method inline, you have to move it.

class Singleton {
public:
    static Singleton & instant();
    // etc..
};

class Dotted : public Singleton {
    // etc..
};

// Now it works:
Singleton & Singleton::instant() {
    // etc..
}


I don't see how this can work. At this point:

instance_ = new Dotted();

Dotted is an incomplete type. With g++ 4.4.1 I get:

error: invalid use of incomplete type 'struct Dotted'

Which compiler are you using?


The first error I'm getting is: strcmp not declared. Hint: it's in the <cstring> (≈ <string.h>) header.

After that the next error is:

instance_ = new Dotted();

"Invalid use of incomplete type."

If you use forward declarations, you must separate the declarations and implementations, so you can do things that require complete types after they are defined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜