Search
 
SCRIPT & CODE EXAMPLE
 

CPP

singleton c++

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
};
Comment

singleton c++

// NOTE in c++ a singleton is equivalent to just a namespace

class Singleton
{
public:
    // Prevents any type of copy or new instance
	Singleton(const Singleton&) = delete;
    void operator=(const Singleton&) = delete;
  
    static Singleton& Get()
    {
    	static Singleton instance;
    	return instance;
    }
  
    static int GetData() { return Get().someData}
private:
    Singleton() {}
	int someData = 1;
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ std::sort 
Cpp :: sleep system function linux c++ 
Cpp :: how print fload wiht 2 decimal in c++ 
Cpp :: getline cpp 
Cpp :: C++ Swap 2 Variables Without Using 3rd Variable 
Cpp :: error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ 
Cpp :: max element in array c++ stl 
Cpp :: c++ fizzbuzz 
Cpp :: remove last index of the string in c++ 
Cpp :: find primes in cpp 
Cpp :: max_element c++ 
Cpp :: cpp insert overload operator 
Cpp :: docker.io : Depends: containerd (= 1.2.6-0ubuntu1~) E: Unable to correct problems, you have held broken packages 
Cpp :: how to add an element to std::map 
Cpp :: coordinate in 1d array 
Cpp :: c++ binary search 
Cpp :: C++ structure (Struct) 
Cpp :: delete dynamic array c++ 
Cpp :: checking if a string has only letters cpp 
Cpp :: stringstream stream number to string 
Cpp :: how to remove a index from a string in cpp 
Cpp :: how to sort vector of struct in c++ 
Cpp :: ascii conversion cpp 
Cpp :: stl vector 
Cpp :: c++ vs g++ 
Cpp :: gettimeofday header file 
Cpp :: c++ string conversion operator 
Cpp :: convert kelvin to Fahrenheit 
Cpp :: gcc suppress warning inline 
Cpp :: SUMOFPROD2 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =