首页 > 技术文章 > C++ 伪私有方法

suui90 2020-06-15 21:29 原文

class Queue
{
private:
     Queue(const Queue & q) : qsize(0) {}    // preemptive definition 伪私有方法
     Queue & operator = (const Queue & q) { return *this; }  // preemptive definition 伪私有方法
    ...
};

 

将方法声明为伪私有方法的作用:

  • 避免了本来将自动生成的默认方法定义。
  • 因为这些方法是私有的,所以不能被广泛使用。
  • 在定义其对象不允许被复制的类时,这种方法也有用。

推荐阅读