Is the copy and swap idiom still useful in C++11

后端 未结 4 792
轮回少年
轮回少年 2020-12-24 14:16

I refer to this question: What is the copy-and-swap idiom?

Effectively, the above answer leads to the following implementation:

class MyClass
{
publi         


        
4条回答
  •  天命终不由人
    2020-12-24 15:01

    for mebest do it like this in C++11:

    class MyClass {
    public:
        MyClass(MyString&& pStr) : str(pStr)) { 
            // call MyString(MyString&& pStr)
        }
        MyClass(const MyString& pStr) : str(pStr)) { 
            // call MyString(const MyString& pStr)
        }
    private:
        MyString const str; // Still const!
    };
    

提交回复
热议问题