以下代码在 Food-destructor 上(/之后)崩溃;如下面的堆栈所示;
6 operator delete() 0xb7e5f4bf
5 std::string::_Rep::_M_destroy() 0xb7ec648b
4 <symbol is not available> 0xb7ec64d0
3 std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() 0xb7ec653e
2 Food::~Food() Main.cpp:126 0x0804c33c
1 main() Main.cpp:199 0x0804c288
Food-ctor 从未被调用,但析构函数是?当“字符串 _text”的资源被释放时,AFAICT 事情变得一团糟,但我似乎无法理解为什么会出错。显然我可以将“string _text”更改为“string* _text”,但我宁愿理解为什么会出错。
class Food {
private:
string _text;
public:
Food(){
cout << "ctor Food" << endl;
}
Food(string name) {
cout << "ctor Food: name=" << name << endl;
}
virtual ~Food() {
cout << "dtor Food" << endl;
}
};
template<typename T>
class Action {
public:
static T eat(int i) {
cout << "Eat: " << i << endl;
}
};
int main() {
auto x = Action<Food>::eat(1);
}