简体   繁体   English

为什么我要将复制构造函数和赋值运算符设为私有并在C ++中实现?

[英]Why would I make copy constructor and assignment operator private and implemented in C++?

Inspired by this question . 灵感来自这个问题

Usually the reason to make copy-constructor and assignment operator private is to make the class non-copyable so that objects can only be created and destroyed, but not copied - most of the times it is because copying them would make no sense. 通常,使复制构造函数和赋值运算符private使类不可复制,以便只能创建和销毁对象,而不能复制 - 大多数情况下,因为复制它们没有意义。 In such cases the copy constructor and the assignment operator are both made private and not implemented - if the class is not copyable then noone should copy. 在这种情况下,复制构造函数和赋值运算符都是private 而不是实现的 - 如果类不可复制,那么没有人应该复制。

Is there a case when copy constructor and assignment operator need to be private and have meaningful implementation at the same time? 复制构造函数和赋值运算符是否需要是private并且同时具有有意义的实现?

My guess is that this can be useful for a class that is holding a list of itself - then it can copy the instances internally. 我的猜测是,这对于持有自己列表的类很有用 - 然后它可以在内部复制实例。 This is really only useful for a class that is both the item and the container: 这实际上只对作为项和容器的类有用:

class MyItems
{
private:
    /* Copy ctor and =operator */
    List list;
public:
    void AddItems(MyItems* items)
    {
        MyItems* added = new MyItems(items);
        list.Add(added);
    }
};

Another thought is to allow cloning in circumstance controlled by the class. 另一个想法是允许在班级控制的情况下进行克隆。 This might be useful when copying can make sense, but only on specific conditions or permissions: 当复制有意义时,这可能很有用,但仅限于特定条件或权限:

class MyClass
{
private:
    /* Copy ctor and =operator */
public:
    MyClass* clone()
    {
        if (canClone)
        {
            MyClass* cloned = new MyClass(this);
            return cloned;
        }
        else
        {
            return NULL;
        }
    }
};

There are two cases that come to mind immediately: 有两种情况立即浮现在脑海中:

  1. friend s: friend

    Say that, as part of your design, you have two highly coupled classes where one needs to be able to copy the other (say, as in a factory model or some such), but you don't want to let the whole world be able to copy it. 比方说,作为你设计的一部分,你有两个高度耦合的类,其中一个需要能够复制另一个(比如,在工厂模型或其他类似),但你不想让整个世界成为能够复制它。

  2. wrappers: 包装:

    Say you want to be able to conditionally clone some element, depending on some internal behavior (eg, depending on some class-stateful condition) - the cleanest way, from a language perspective - is still to separate the copying into its own function. 假设您希望能够有条件地克隆某些元素,具体取决于某些内部行为(例如,取决于某些类状态条件) - 从语言角度来看,最简洁的方法仍然是将复制分离为自己的函数。 This would allow for good separation of concerns. 这样可以很好地分离关注点。

  1. We make copy constructor and operator = unimplemented so that even a friend cannot have any access to it. 我们使复制构造函数和operator =未实现,以便即使是friend也无法访问它。 If you implement, it means you want a friend to have access. 如果您实施,则意味着您希望friend有权访问。 This is a design decision. 这是一个设计决定。
  2. You want to do explicit cloning ; 你想做明确的克隆 ; ie allow copying but also make its code "dirty" looking (something like C++ style casting operations, which shows dirt in your code ) 即允许复制,但也使其代码“脏”看起来(类似于C ++样式的转换操作, 在代码中显示污垢

eg 例如

class A {
  A(const A& obj) { ... }
  A& operator = (const A& obj) { ... }
public:
  A& clone(const A& obj)
  {
    *this = obj;
    return *this;
  }
};

We have put this wrapper clone() to allow the user for cloning, however it also displays explicitly what exactly he/she is doing. 我们已经使用这个包装器clone()来允许用户进行克隆,但是它也明确地显示了他/她正在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM