简体   繁体   English

深层应对c ++ - 如果给出了指向该类的指针,则复制一个类

[英]Deep coping c++ - To Make a copy of a class if pointer to that class is given

I am new to copy constructor concept .I have a basic question. 我是新手复制构造函数的概念。我有一个基本的问题。 Wanted to implement a function like 想要实现像这样的功能

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
   }

I know in this way we can invoke copy constructor.But if pointer to o ie *o is given how to invoke copy constructor and do a deep copy. 我知道通过这种方式我们可以调用复制构造函数。但是如果指向o ie * o的指针如何调用复制构造函数并执行深层复制。

Then dereference o : 然后取消引用o

orig* o = new orig("Hello");
orig dup(*o);

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

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