如果给出了该课程的指针,则深入应对以制作课程的副本
Deep coping To Make a copy of a class if pointer to that class is given
我是复制构造函数概念的新手。我有一个基本问题。想要实现
之类的函数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);
相关文章:
- 如果没有malloc,链表实现将失败
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 如果编译的源代码是特定于它编译的硬件的,我们如何分发它
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- C++擦除(如果存在)
- 如果静态变量只为程序的整个部分存储了一个副本,为什么我不能使用静态变量交换 2 个数字?
- 为什么在C 11中推荐的值(如果需要副本),如果const引用也只花费单个副本,则建议通过值(如果需要副本)
- 如果给出了该课程的指针,则深入应对以制作课程的副本
- 在一个映射向量上迭代,如果满足条件,则插入一个副本
- 如何制作初始文件的 10 个副本,如果第一个文件是 AS-1.txt第二个应该是 AS-2.txt依此类推
- 如果参数创建本地副本,则使用参数"const [variable_type] &"的目的是什么?
- 如果我需要将重的东西传递给对象,我应该通过指针还是通过副本传递
- 如果引发的异常始终是异常对象的副本,为什么不调用此复制构造函数?
- 为什么将CString类型转换为wchar_t*会产生一个临时副本?如果我们使用其他类型而不是CString呢?
- Objective-C:如果我在ARC的函数中返回一个c++对象,它会创建一个副本吗?我是否必须手动释放该副本?