下面是一个返回局部变量的例子,我们知道在编程中返回局部变量会引发很多问题,但实际编程的时候可能由于编译器的“友好”而没有检测到这种错误。
class MyClass {
private:
const int DEFAULT_SIZE = 100;
public:
MyClass() = default;
MyClass function() {
member = new int[DEFAULT_SIZE];
return *this;
}
private:
int* member = nullptr;
};
**的赋值问题
#include <iostream>
using namespace std;
const int DEFAULT_SIZE = 10;
void function(int *a) { a = new int[DEFAULT_SIZE]; }
int main(int argc, const char **argv) {
int *a;
function(a);
a[2] = 1;
system("pause");
return 0;
}