本文介绍了如何使这个构造函数的深拷贝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了这个构造函数,我需要做一个深的副本。我真的不明白一个深的副本的意思。我知道它使一个对象的独立副本有自己的动态内存,但我不明白这是需要什么。我也不知道如何实际实现深拷贝。任何建议?

I made this constructor, and I need to make a deep copy of it. I don't really understand the meaning of a deep copy. I know it makes independent copy of an object that has its own dynamic memory, but I don't understand what the need is for this. I'm also not sure how to actually implement the deep copy. Any suggestions?

这里是我的构造函数,我需要做的深拷贝:
任何人都可以提供一些语法帮助,像一个骨架?

Here is my constructor that I need to make deep copy of:Could anyone provide some syntax help, like a skeleton?

template<class t_type>
inline ALIST<t_type>::ALIST()
{
  t_type value;
  capacity=10;
  DB = new t_type[capacity];
  count=capacity;

  cout<<"Enter value: ";
  cin.clear();
  cin>>value;


  for(int i=0; i<capacity; i++)
  {
    DB[i]=value;
  }
}


推荐答案

a href =http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom>什么是复制和交换惯用语?

What is the copy-and-swap idiom?

使用深度副本,以便您可以复制对象具有的所有信息,然后更改副本而不更改原始副本。一般来说,浅拷贝只会复制指针本身,但仍指向相同的信息。如果您不小心,这可能会导致数据发生意外更改。

Deep copies are used so that you can copy all the information that an object has, and then alter the copy without altering the original. Broadly speaking, a shallow copy will copy only the pointer itself, but still point to the same information. This can lead to unexpected changes in data if you're not careful.

这篇关于如何使这个构造函数的深拷贝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:19