619

Вопрос по c++ – C ++ Singleton дизайн шаблона

Недавно я столкнулся с реализацией / реализацией шаблона проектирования Singleton для C ++. Это выглядело так (я взял это из примера из реальной жизни): // a lot of methods are omitted here class Singleton { public: static ...

Error: User Rate Limit Exceeded<a href="http://www.aristeia.com/Papers/DDJ%5FJul%5FAug%5F2004%5Frevised.pdf" rel="nofollow noreferrer">aristeia.com/Papers/DDJ%5FJul%5FAug%5F2004%5Frevised.pdf</a>

от Matthieu N.

Error: User Rate Limit Exceeded<b>bad</b>Error: User Rate Limit Exceeded

от derekerdmann

Error: User Rate Limit Exceeded<a href="http://stackoverflow.com/questions/211237/c-static-variables-initialisation-order/211307#211307" title="c static variables initialisation order">stackoverflow.com/questions/211237/&#x2026;</a>Error: User Rate Limit Exceeded<a href="http://stackoverflow.com/questions/270947/can-any-one-provide-me-a-sample-of-singleton-in-c/271104#271104" title="can any one provide me a sample of singleton in c">stackoverflow.com/questions/270947/&#x2026;</a>Error: User Rate Limit Exceeded<a href="http://stackoverflow.com/questions/246564/what-is-the-lifetime-of-a-static-variable-in-a-c-function" title="what is the lifetime of a static variable in a c function">stackoverflow.com/questions/246564/&#x2026;</a>Error: User Rate Limit Exceeded<a href="http://stackoverflow.com/questions/449436/singleton-instance-declared-as-static-variable-of-getinstance-method/449823#449823" title="singleton instance declared as static variable of getinstance method">stackoverflow.com/questions/449436/&#x2026;</a>Error: User Rate Limit Exceeded<a href="http://stackoverflow.com/questions/335369/finding-c-static-initialization-order-problems/335746#335746" title="finding c static initialization order problems">stackoverflow.com/questions/335369/&#x2026;</a>

от Martin York

Error: User Rate Limit Exceeded

от jwd

Error: User Rate Limit Exceeded<i>sometimes</i>Error: User Rate Limit Exceeded<i>hack</i>Error: User Rate Limit Exceeded

от sbi

18 ответов

-2

Error: User Rate Limit Exceeded

11

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

Error: User Rate Limit ExceededsingletonError: User Rate Limit ExceededsingletonError: User Rate Limit Exceeded

Error: User Rate Limit Exceededstd::shared_ptrError: User Rate Limit ExceededsingletonError: User Rate Limit Exceeded

class Singleton
{
public:
    Singleton(Singleton const&) = delete;
    Singleton& operator=(Singleton const&) = delete;

    static std::shared_ptr<Singleton> instance()
    {
        static std::shared_ptr<Singleton> s{new Singleton};
        return s;
    }

private:
    Singleton() {}
};
6

Error: User Rate Limit Exceededmain()Error: User Rate Limit Exceededmain.

Error: User Rate Limit ExceededQApplicationError: User Rate Limit ExceededmainError: User Rate Limit Exceeded

Error: User Rate Limit Exceededinit()Error: User Rate Limit Exceededterm()Error: User Rate Limit ExceededmainError: User Rate Limit ExceededgetInstance()Error: User Rate Limit Exceeded

941

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededError: User Rate Limit Exceeded.

class S
{
    public:
        static S& getInstance()
        {
            static S    instance; // Guaranteed to be destroyed.
                                  // Instantiated on first use.
            return instance;
        }
    private:
        S() {}                    // Constructor? (the {} brackets) are needed here.

        // C++ 03
        // ========
        // Don't forget to declare these two. You want to make sure they
        // are unacceptable otherwise you may accidentally get copies of
        // your singleton appearing.
        S(S const&);              // Don't Implement
        void operator=(S const&); // Don't implement

        // C++ 11
        // =======
        // We can use the better technique of deleting the methods
        // we don't want.
    public:
        S(S const&)               = delete;
        void operator=(S const&)  = delete;

        // Note: Scott Meyers mentions in his Effective Modern
        //       C++ book, that deleted functions should generally
        //       be public as it results in better error messages
        //       due to the compilers behavior to check accessibility
        //       before deleted status
};

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

1

Error: User Rate Limit Exceededstd::call_onceError: User Rate Limit Exceededstd::once_flag? Most other approaches - including double checked locking - are broken.

Error: User Rate Limit Exceededstd::once_flagError: User Rate Limit Exceeded

3

Error: User Rate Limit Exceeded

#include <Windows.h>
#include <iostream>

using namespace std;


class SingletonClass {

public:
    static SingletonClass* getInstance() {

    return (!m_instanceSingleton) ?
        m_instanceSingleton = new SingletonClass : 
        m_instanceSingleton;
    }

private:
    // private constructor and destructor
    SingletonClass() { cout << "SingletonClass instance created!\n"; }
    ~SingletonClass() {}

    // private copy constructor and assignment operator
    SingletonClass(const SingletonClass&);
    SingletonClass& operator=(const SingletonClass&);

    static SingletonClass *m_instanceSingleton;
};

SingletonClass* SingletonClass::m_instanceSingleton = nullptr;



int main(int argc, const char * argv[]) {

    SingletonClass *singleton;
    singleton = singleton->getInstance();
    cout << singleton << endl;

    // Another object gets the reference of the first object!
    SingletonClass *anotherSingleton;
    anotherSingleton = anotherSingleton->getInstance();
    cout << anotherSingleton << endl;

    Sleep(5000);

    return 0;
}

Error: User Rate Limit Exceeded

SingletonClass instance created!
00915CB8
00915CB8

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

-1

Error: User Rate Limit Exceeded

-1

#define INS(c) private:void operator=(c const&){};public:static c& I(){static c _instance;return _instance;}

Error: User Rate Limit Exceeded

   class CCtrl
    {
    private:
        CCtrl(void);
        virtual ~CCtrl(void);

    public:
        INS(CCtrl);
0

Error: User Rate Limit ExceededError: User Rate Limit Exceeded

-5

Error: User Rate Limit Exceeded

class singleton
{
    static singleton *s;
    static unsigned char *buffer[sizeof(singleton)/4 *4] //4 byte align
    static singleton* getinstance()
    {
        if (s == null)
        {
            s = new(buffer) singleton;
        }
        return s;
    }
};
2

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Singleton * Singleton::getInstance() {
    if (!instance) {
        instance = new Singleton();
    };
    return instance;
};

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

5

Error: User Rate Limit Exceeded

class S
{
    public:
        static S& getInstance()
        {
            if( m_s.get() == 0 )
            {
              m_s.reset( new S() );
            }
            return *m_s;
        }

    private:
        static std::unique_ptr<S> m_s;

        S();
        S(S const&);            // Don't Implement
        void operator=(S const&); // Don't implement
};

std::unique_ptr<S> S::m_s(0);
44

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

1

Error: User Rate Limit Exceeded

struct Store{
   std::array<Something, 1024> data;
   size_t get(size_t idx){ /* ... */ }
   void incr_ref(size_t idx){ /* ... */}
   void decr_ref(size_t idx){ /* ... */}
};

template<Store* store_p>
struct ItemRef{
   size_t idx;
   auto get(){ return store_p->get(idx); };
   ItemRef() { store_p->incr_ref(idx); };
   ~ItemRef() { store_p->decr_ref(idx); };
};

Store store1_g;
Store store2_g; // we don't restrict the number of global Store instances

Error: User Rate Limit ExceededmainError: User Rate Limit Exceeded

auto ref1_a = ItemRef<&store1_g>(101);
auto ref2_a = ItemRef<&store2_g>(201); 

Error: User Rate Limit ExceededStoreError: User Rate Limit ExceededStoreError: User Rate Limit ExceededStoreError: User Rate Limit ExceededItemRefError: User Rate Limit ExceededfriendError: User Rate Limit ExceededStoreError: User Rate Limit Exceeded

Error: User Rate Limit ExceededStoreError: User Rate Limit Exceeded

template <typename Store_t, Store_t* store_p>
struct StoreWrapper{ /* stuff to access store_p, e.g. methods returning 
                       instances of ItemRef<Store_t, store_p>. */ };

Error: User Rate Limit ExceededStoreWrapperError: User Rate Limit ExceededStoreError: User Rate Limit ExceededStore).

-1

Error: User Rate Limit Exceeded

#ifndef SC_SINGLETON_CLASS_H
#define SC_SINGLETON_CLASS_H

class SingletonClass
{
    public:
        static SingletonClass* Instance()
        {
           static SingletonClass* instance = new SingletonClass();
           return instance;
        }

        void Relocate(int X, int Y, int Z);

    private:
        SingletonClass();
        ~SingletonClass();
};

#define sSingletonClass SingletonClass::Instance()

#endif

Error: User Rate Limit Exceeded

sSingletonClass->Relocate(1, 2, 5);
33

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

class Singleton
{
private:
   Singleton();

public:
   static Singleton& instance()
   {
      static Singleton INSTANCE;
      return INSTANCE;
   }
};

Error: User Rate Limit Exceeded

2

Error: User Rate Limit Exceeded

template<typename HeirT>
class Singleton
{
public:
    Singleton() = delete;

    Singleton(const Singleton &) = delete;

    Singleton &operator=(const Singleton &) = delete;

    static HeirT &instance()
    {
        static HeirT instance;
        return instance;
    }
};

Error: User Rate Limit Exceededclass Test : public Singleton<Test>

9

Error: User Rate Limit ExceededCError: User Rate Limit Exceeded

singleton<C>()

Error: User Rate Limit Exceeded

template <class X>
X& singleton()
{
    static X x;
    return x;
}

Error: User Rate Limit Exceeded

Похожие вопросы