2012-12-24 3 views
0

다음과 같은 코드가 Food-destructor에서 충돌합니다. 아래의 스택과 같이;C++ 템플릿, 정적 메서드 및 생성자

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는 호출되지 않지만 소멸자는 무엇입니까? AFAICT는 "string _text"리소스가 해제 될 때 건방진 상태가되지만 왜 이것이 잘못 될지 이해할 수 없습니다. 분명히 "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); 
} 
+1

'static T eat (int i)'이 가정 된'T'를 반환하지 않기 때문에 프로그램이 컴파일되지 않습니다. 참으로 – user93353

답변

4

정의되지 않은 동작입니다. 함수 (eat)는 T 유형을 반환하지만 실제로는 이 아니며을 반환합니다. 이로 인해 지정이 정의되지 않습니다.

+0

. 바꾸기 : 'static T eat (int i) { T 인스턴스 (문자열 ("Appel")); cout << "먹기 :"<< i << endl; return instance; }' 완벽하게 작동합니다. – DogGuts