浅拷贝
只是对指针的拷贝,拷贝后两个指针指向同一个内存空间;
深拷贝
对指针指向的内容进行拷贝(重新分配内存),经深拷贝后的指针是指向不同地址的指针;
因此浅拷贝释放内存的时候很容易出现因为释放两个指针而内存出错。
浅拷贝(释放时,因为多次释放出错)
只拷贝指针
- //拷贝构造函数
- Vector(const Vector<T>& v)
- :_start(nullptr)
- ,_finish(nullptr)
- ,_endOfStorage(nullptr)
- {
- _start=v._start;
- _finish=v._finish;
- _endOfStorage=v._endOfStorage;
- }
深拷贝
对资源进行拷贝
- Vector(const Vector<T>& v)
-
- :_start(nullptr)
- , _finish(nullptr)
- , _endOfStorage(nullptr)
- {
- size_t n = v.capacity();
- _start = new T[n];
- for (size_t i = 0; i < v.size(); ++i)
- {
- _start[i] = v[i];
- }
- _finish = _start + v.size();
- _endOfStorage = _start + n;
- }
写一个Vector的类
- template<class T>
- class Vector
- {
- typedef T* operator;
- typedef const T* const_iterator;
- iterator _start;
- iterator _finish;
- iterator _endOfStorage;
-
- public:
- //构造函数
- Vector()
- :_start(nullptr)
- , _finish(nullptr)
- , _endOfStorage(nullptr)
- {}
-
- //析构函数
- ~Vector()
- {
- if(_start)
- {
- delete[] _start;
- _star=_finish=_endOfStorage=nullptr;
- }
- }
- T& operator[](size_t pos)
- {
- if (pos >= 0 && pos < size())
- return _start[pos];
- }
- size_t size() const
- {
- return _finish - _start;
- }
-
- size_t capacity() const
- {
- return _endOfStorage - _start;
- }
-
-
- };
可以用自己编辑器,把拷贝放进去试试;
附:c++深拷贝与浅拷贝问题实例
浅拷贝:简单的赋值拷贝操作;
深拷贝:在堆区重新申请空间,再进行拷贝操作;
问题:浅拷贝会带来堆区内存被重复释放的问题,析构函数被调用多次,导致程序运行崩溃;
解决:通过深拷贝解决,在堆区重新申请内存,各自释放自己的内存,避免重复释放;
- #include <iostream>
-
- using namespace std;
-
- class Person
- {
- public:
- Person() {
- cout << "Person的默认构造函数调用"<<endl;
- }
- Person(int age,int height) {
- m_Age = age;
- m_Height = new int(height);//堆区重新申请空间,进行深拷贝,手动申请,手动释放;
- cout << "Person的有参函数调用" << endl;
- }
- int m_Age;
- int *m_Height;
-
- //自己实现拷贝构造函数,来避免编译器的拷贝构造函数造成浅拷贝问题;
- Person(const Person& p) {
- cout << "Person拷贝构造函数" << endl;
- m_Age = p.m_Age;
- //m_Height = p.m_Height; 浅拷贝,编译器默认实现这行代码;
- m_Height = new int(*p.m_Height);//深拷贝
- }
-
- ~Person() {
- //析构代码,将堆区开辟数据做释放操作
- if (m_Height != NULL) {
- delete m_Height;
- m_Height = NULL;
- }
- cout << "Person的析构函数调用" << endl;
- }
- };
-
- void test01(){
- Person p1(18,160);
- cout << "p1的年龄为:" << p1.m_Age<<"p1身高为:"<<*p1.m_Height<< endl;
- Person p2(p1);//编译器默认调用拷贝构造函数,进行浅拷贝操作
- cout << "p2的年龄为:" << p2.m_Age<< "p2身高为:"<<*p2.m_Height << endl;
- }
-
- int main(){
- test01();
- system("pause");
- }
程序运行结果:

总结
到此这篇关于C++深拷贝与浅拷贝区别及应用的文章就介绍到这了,更多相关C++深拷贝与浅拷贝内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!