gpt4 book ai didi

c++ - 深度复制 c++ - 如果给出指向该类的指针,则复制该类

转载 作者:行者123 更新时间:2023-11-28 03:29:02 25 4
gpt4 key购买 nike

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

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 即 *o 的指针被赋予如何调用复制构造函数并进行深度复制。

最佳答案

然后取消引用o:

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

关于c++ - 深度复制 c++ - 如果给出指向该类的指针,则复制该类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13117523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com