class Test {
Test test;
};
error C2460: “Test::test”: 使用正在定义的“Test”
struct Test {
Test test;
};
这种定义不应该存在,即使语法不存在问题,运行的时候也会出现问题。如果创建变量的时候,一定会先构造test对象,但是构造test的对象的时候,有需要构造test.test对象,构造test.test的对象的时候,需要先构造test.test.test对象。
#include<iostream>
using namespace std;
class Test {
Test* test;
public:
Test() {
cout << "Test" << endl;
test = new Test();
}
};
#include "Test.h"
int main() {
Test test;
return 0;
}
出现stack overflow异常。
打印异常:
#include "Test.h"
int main() {
try {
Test test;
}
catch (exception e) {
cout << e.what() << endl;
}
return 0;
}
EXCEPTION_STACK_OVERFLOW不是C++异常,只能用操作系统提供的结构化异常(Structured Exception Handling,SEH)措施来处理。
#include<Windows.h>
#include"Test.h"
int main() {
__try {
// 导致栈溢出的操作
Test test;
}
__except ((GetExceptionCode() == EXCEPTION_STACK_OVERFLOW) ?
EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
{
int reset = _resetstkoflw();
cout << "exception captured!" << endl;
}
}
也可以把SEH转换为std::exception,结合std::exception 和SEh
开启/Eha选项
C++异常和SEH的结合_郁郁 -CSDN博客https://blog.csdn.net/chenyu2202863/article/details/3260345