手动定义复制构造函数和赋值运算符的一部分

时间:2012-09-05 16:59:09

标签: c++ copy-constructor assignment-operator

我想知道是否有办法实现复制构造函数和赋值运算符,以便在为类重新定义时只需要进行少量修改。

例如,考虑一个类:

class Foo {
private:
    int* mInt_ptr;
    /* many other member variables
       of different types that aren't
       pointers */
public:
    Foo();
    Foo(const Foo&);
    Foo& operator=(const Foo&);
    ~Foo();
};

现在,为了处理指针mInt_ptr,我需要在复制构造函数和赋值运算符中正确处理它。但是,其余的成员变量可以安全地进行浅层复制。有没有办法自动执行此操作?

一旦一个类变大,显式写出复制非指针成员变量的操作可能会变得乏味和笨重,所以我想知道是否有一种方法可以写一个复制构造函数,例如:

Foo::Foo(const Foo& tocopy)
{
    mInt_ptr = new int(*tocopy.mInt_ptr);
    /* Do shallow copy here somehow? */
}

而不是明确的形式:

Foo::Foo(const Foo& tocopy)
{
    mInt_ptr = new int(*tocopy.mInt_ptr);
    mVar1 = tocopy.mVar1;
    mVar2 = tocopy.mVar2;
    ...
    ...
    mVarN = tocopy.mVarN;
}

3 个答案:

答案 0 :(得分:4)

一般来说,不要使用原始指针,正是因为你正在与之抗争。而是使用合适的智能指针,并使用复制交换分配:

class Foo
{
     int a;
     Zip z;
     std::string name;
     value_ptr<Bar> p;

public:
     Foo(Foo const &) = default;

     Foo & operator=(Foo rhs)
     {
         rhs.swap(*this);
         return *this;
     }

     void swap(Foo & rhs)
     {
         using std::swap;
         swap(a, rhs.a);
         swap(z, rhs.z);
         swap(name, rhs.name);
         swap(p, rhs.p);
     }
};

namespace std { template <> void swap<Foo>(Foo & a, Foo & b) { a.swap(b); } }

value_ptr可以是full-blown implementation,或者像这样简单的东西:

template <typename T>    // suitable for small children,
class value_ptr          // but not polymorphic base classes.
{
    T * ptr;

public:
    constexpr value_ptr() : ptr(nullptr) { }
    value_ptr(T * p) noexcept : ptr(p) { }
    value_ptr(value_ptr const & rhs) : ptr(::new T(*rhs.ptr)) { }
    ~value_ptr() { delete ptr; }
    value_ptr & operator=(value_ptr rhs) { rhs.swap(*this); return *this; }
    void swap(value_ptr & rhs) { std::swap(ptr, rhs.ptr); }

    T & operator*() { return *ptr; }
    T * operator->() { return ptr; }
};

答案 1 :(得分:2)

如何在一个小的helper结构中包装所有浅拷贝位并使用那里的默认拷贝行为。

class Foo {
private:
    int* mInt_ptr;
    struct helper_t
    /* many other member variables
       of different types that aren't
       pointers */
    } mHelper;
public:
    Foo();
    Foo(const Foo&);
    Foo& operator=(const Foo&);
    ~Foo();
};

Foo::Foo(const Foo& tocopy)
{
    mInt_ptr = new int(*tocopy.mInt_ptr);
    mHelper = tocopy.mHelper;
}

如克雷克所建议的那样,使用更好的原语似乎更好的设计。这只是另一种可能性。

答案 2 :(得分:1)

无论你使用原始指针还是智能指针,Kerrek的解决方案都是正确的,你应该使用它们来制作复制构造函数,析构函数和交换并实现赋值:

class Foo 
{
private:
    int* mInt_ptr;
    // many other member variables
    // of different types
public:
    Foo()
        : mInt_ptr(NULL)
        // initialize all other members
    {}
    Foo(const Foo& that)
        : mInt_ptr(new int(*that.mInt_ptr) )
        // copy-construct all other members
    {}
    Foo& operator=(const Foo& that)
    {
        // you may check if(this == &that) here
        Foo(that).swap(*this);
        return *this;
    }
    ~Foo()
    {
        delete mInt_ptr;
        // and release other resources
    }
    void swap(Foo& that)
    {
        std::swap(mInt_ptr, that.mInt_ptr);
        // swap all members
    }
};

这里的成员只是为了保持紧凑,通常不建议使用内联成员定义来加重类定义。