-2

I am exploring design patterns in cpp, in singleton pattern we usually saw this piece of code

// Delete copy/move so extra instances can't be created/moved.
  Singleton(const Singleton&) = delete;
  Singleton& operator=(const Singleton&) = delete;
  Singleton(Singleton&&) = delete;
  Singleton& operator=(Singleton&&) = delete;

didn't see any good explanation of what these part of code are doing? Is it possible to get a line by line explanation of what each line is doing?

ganeshredcobra
  • 1,881
  • 3
  • 28
  • 44
  • 4
    They are removing the ability to copy (what good is a singleton if you can make more than one?) and move (the singleton's `get_instance` function should not give up ownership of the singleton) the singleton. – user4581301 Oct 27 '21 at 21:21
  • 1
    Here is a near-miss duplicate: [Disable copy constructor](https://stackoverflow.com/questions/6077143/disable-copy-constructor) – user4581301 Oct 27 '21 at 21:23
  • Note: You really only need to explicitly delete the copy special member functions. Deleting a special member function counts as declaring the function and as soon as you declare the copy function, the automatic generation of the matching move function is disabled. – user4581301 Oct 27 '21 at 21:32
  • 3
    @user4581301 Perhaps nice to make it explicit though, it makes your intentions clear. – Paul Sanders Oct 27 '21 at 21:36
  • 1
    You obviously need to read one or two books on C++ 11 or later. By the way, the comment above the code is **very clear**. – Phil1970 Oct 27 '21 at 22:17

2 Answers2

1

The delete keyword in this context tells the compiler not to create the constructor and assignment/move methods. It's needed because the compiler will create them by default if you don't declare them. They're called "special member functions" for this reason.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
VorpalSword
  • 1,223
  • 2
  • 10
  • 25
  • 1
    Do note that `delete`'d member functions still exist, they just won't have a default implementation provided by the compiler. For instance, they will still be visible to Overload Resolution, and will cause a compilation error if the resolution selects a `delete`'d function as the best match. – Remy Lebeau Oct 28 '21 at 01:05
0

yes i definitely takes your comments that i should read more books or learn more, but i got a better explanation of above code like what each of those lines are preventing

#include <iostream>
#include <vector>

using namespace std;

class A
{
    public:
        int n;
        A(int n = 1) : n(n) { }
        /*
        A ( A && obj){
            cout << "Calling Move constructor\n";
        }*/

        /*
        A& operator= (A&& x) {
          cout << "Move constructor = operator\n";
          return *this;
        }*/

        void print(string k){cout<<"text is "<<k<<" num is "<<n<<endl;}

        /****1->****/  
        A(const A&) = delete;
        /*****2->*****/
        A& operator=(const A&) = delete;
        
        /*****3->*****/
        A(A&&) = delete;
        /*****4->*****/
        A& operator=(A&&) = delete;
};


int main()
{
    A a1(1);
    a1.print("a1");
    
    /****1->****/ 
    //A a2(a1);
    
    //a2.print("a2");
    /*****2->*****/ 
    //A a3;
        //a3=a1;
    //a3.print("a3");
    
    vector <A> vec;
    /*****3->*****/ 
    //vec.push_back(A());
    //A a4=(std::move(a1));
    
    /*****4->*****/
     //A a5,a6;
    //a5=std::move(a6);

    return 0;
}
ganeshredcobra
  • 1,881
  • 3
  • 28
  • 44