简体   繁体   中英

about copy constructor and overloaded assignment operator

I would like to know in which situations should a copy constructor or assignment operator should be defined. From what I researhed I see that whenever value is passed by value or returned by value, the copy constructor and overloaded assignment operator needs to be defined. However, when pointers or reference are used for pass/return by pointer or reference(&), do we need copy constructor/overloaded assignment operator

class Sample
{
 public :
  // Assume a constructor that sets the node member
  SampleNode * getNode()
  {
    return _node;
  }
 private: 
  SampleNode * node;
}

class SampleNode
{
  public:
   void getValue()
   {
      return _value;
   }
  private: 
   unsigned int value;
}

main()
{
  Sample * ptr = new Sample(15);

  SampleNode *node = getNode(); // Do we need a copy constructor here?
}

定义指向对象的指针或引用不需要使用任何构造函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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