我想知道应该在哪些情况下定义复制构造函数或赋值运算符。从我的研究中我看到,每当值是按值传递或按值返回时,都需要定义复制构造函数和重载赋值运算符。但是,当指针或引用用于通过指针或引用(&)传递/返回时,我们是否需要复制构造函数/重载赋值运算符
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?
}