如果给出了该课程的指针,则深入应对以制作课程的副本

Deep coping To Make a copy of a class if pointer to that class is given

本文关键字:副本 如果 指针      更新时间:2023-10-16

我是复制构造函数概念的新手。我有一个基本问题。想要实现

之类的函数
orig *f1(orig*o)
{
  // Returns a copy of *0 and should deep copy all the values of the parent
  // pointer.Planning to implement a copy constructor to achieve the same.
  // Can anyone provide a prototype or some idea on the same?
}
class dummyclass
{
 int value;
};
class orig
{
  dummyclass *dummy;
  char str[100];
public:
 //default constructor
:
//parametrised constructor
 orig(char *p)
{
   dummy = new dummyclass;
  //rest of the initialisation
}
orig(const orig& duplicate)
{
//copy constructor
}
};
int main()
{
  orig o("Hello");//constructor
  orig dup(o);//copy constructor
   }

我知道我们可以调用复制构造函数。但是,如果指向o IE *o的指针如何调用复制构造函数并进行深层复制。

然后解除o

orig* o = new orig("Hello");
orig dup(*o);
相关文章: