Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
浅拷贝不崩溃 在深入研究C++的同时,我正在进行浅/深的复制。根据我跟踪的MOOC,以下代码应该崩溃 // .hpp file #ifndef _COPY_HPP_ #define __COPY_HPP_ class Shallow { private: int* data; public: void setData (int d){ *data = d;} int getData(){ return *data;} // constructor Shallow (int d); // copy constructor Shallow(const Shallow &source); //destructor ~Shallow(); }; #endif // .cpp file #include "copy.hpp" #include<stdio.h> #include<iostream> using namespace std; Shallow::Shallow(int d){ data = new int; *data = d; } void displayShallow(Shallow s){ cout << "displayShallow: " << s.getData() << "\n"; } Shallow::Shallow(const Shallow &source) : data{source.data} { cout << "copy constructor call" << "\n"; } Shallow::~Shallow(){ delete data; cout << "freeing data" << "\n"; } int main(){ Shallow original{100}; cout << original.getData() << "\n"; // destructor called after this call displayShallow(original); cout << original.getData() << "\n"; original.setData(29); cout << original.getData() << "\n"; return 0; }_C++ - Fatal编程技术网

浅拷贝不崩溃 在深入研究C++的同时,我正在进行浅/深的复制。根据我跟踪的MOOC,以下代码应该崩溃 // .hpp file #ifndef _COPY_HPP_ #define __COPY_HPP_ class Shallow { private: int* data; public: void setData (int d){ *data = d;} int getData(){ return *data;} // constructor Shallow (int d); // copy constructor Shallow(const Shallow &source); //destructor ~Shallow(); }; #endif // .cpp file #include "copy.hpp" #include<stdio.h> #include<iostream> using namespace std; Shallow::Shallow(int d){ data = new int; *data = d; } void displayShallow(Shallow s){ cout << "displayShallow: " << s.getData() << "\n"; } Shallow::Shallow(const Shallow &source) : data{source.data} { cout << "copy constructor call" << "\n"; } Shallow::~Shallow(){ delete data; cout << "freeing data" << "\n"; } int main(){ Shallow original{100}; cout << original.getData() << "\n"; // destructor called after this call displayShallow(original); cout << original.getData() << "\n"; original.setData(29); cout << original.getData() << "\n"; return 0; }

浅拷贝不崩溃 在深入研究C++的同时,我正在进行浅/深的复制。根据我跟踪的MOOC,以下代码应该崩溃 // .hpp file #ifndef _COPY_HPP_ #define __COPY_HPP_ class Shallow { private: int* data; public: void setData (int d){ *data = d;} int getData(){ return *data;} // constructor Shallow (int d); // copy constructor Shallow(const Shallow &source); //destructor ~Shallow(); }; #endif // .cpp file #include "copy.hpp" #include<stdio.h> #include<iostream> using namespace std; Shallow::Shallow(int d){ data = new int; *data = d; } void displayShallow(Shallow s){ cout << "displayShallow: " << s.getData() << "\n"; } Shallow::Shallow(const Shallow &source) : data{source.data} { cout << "copy constructor call" << "\n"; } Shallow::~Shallow(){ delete data; cout << "freeing data" << "\n"; } int main(){ Shallow original{100}; cout << original.getData() << "\n"; // destructor called after this call displayShallow(original); cout << original.getData() << "\n"; original.setData(29); cout << original.getData() << "\n"; return 0; },c++,C++,似乎数据指向的存储被设置为0,而不是崩溃。我是否正确理解?< /p>“未定义的行为”是指“它可能崩溃,它可能不会崩溃,它可能释放愤怒”,根据MOOC——什么是“MOOC”?这是另一个在线C++课程给出错误信息。这样的保证有什么用?通常的误解是,在C++中调用UB将强制导致崩溃。不幸的是,情况并非如此。这很好,但它会花费成本,C++有范例——不为你不使用的东西付费。 100 copy constructor call displayShallow: 100 freeing data 0 29 fr

似乎
数据
指向的存储被设置为
0
,而不是崩溃。我是否正确理解?< /p>“未定义的行为”是指“它可能崩溃,它可能不会崩溃,它可能释放愤怒”,根据MOOC——什么是“MOOC”?这是另一个在线C++课程给出错误信息。这样的保证有什么用?通常的误解是,在C++中调用UB将强制导致崩溃。不幸的是,情况并非如此。这很好,但它会花费成本,C++有范例——不为你不使用的东西付费。
100
copy constructor call
displayShallow: 100
freeing data
0
29
freeing data