아래 예외 클래스가 있습니다.왜 아래 코드가 VS2010에서 성공적으로 컴파일됩니까?
class ExceptionTest : std::exception
{
public:
ExceptionTest(int value):
m_value(value)
{
}
~ExceptionTest()
{
}
private:
ExceptionTest(const ExceptionTest& test)
{
}
int m_value;
};
그때 나는 이런 식으로 사용 -
은int checkexception()
{
throw ExceptionTest(2);
}
int main()
{
try
{
checkexception();
}
catch (ExceptionTest& exception)
{
cout<<"haha";
}
return 1;
}
이 복사 생성자가 개인 인 경우에도 완벽하게 잘 작동합니다. 당신이 값으로 예외를 잡을 경우
실패 -
int main()
{
try
{
checkexception();
}
catch (ExceptionTest exception) --> this fails
{
cout<<"haha";
}
return 1;
}
내가 오류는 I 클래스
에 복사 생성자를 정의하지 않는 경우error C2316: 'ExceptionTest' : cannot be caught as the destructor and/or copy
constructor are inaccessible
내가 링커 오류입니다
class ExceptionTest : std::exception
{
public:
ExceptionTest(int value):
m_value(value)
{
}
~ExceptionTest()
{
}
private:
ExceptionTest(const ExceptionTest& test);
int m_value;
};
링크 : C : \ Users \ sumitha \ Documents \ Visual Studio 2010 \ Projects \ test \ Debug \ test.exe not fo 마지막 증분 링크에 의해 생성되거나 생성되지 않음; 전체 링크 수행 1> main.obj : 오류 LNK2001 : 해결되지 않은 외부 기호 "개인 : __thiscall ExceptionTest :: ExceptionTest (클래스 ExceptionTest const &)"(0ExceptionTest @@ AAE @ ABV0 @@ Z) 1> C : \ Users \ sumitha \ Documents \ Visual Studio 2010 \ Projects \ test \ Debug \ test.exe : 치명적인 오류 LNK1120 : 해결되지 않은 외부가 ========== 빌드 : 0 성공, 1 실패, 0 up-to -date, 0 skipped ==========
위의 내용이 참이면 예외 클래스의 복사본 생성자를 항상 private으로 설정하여 호출자가 예외를 catch하도록 할 수 있습니다. 참고. 나는 이것이 "반환 가치 최적화"로 인해 일어나고 있다고 생각한다.
저는 C + + 숙달이 아니기 때문에 & nbsp 원래의 예외에 대한 참조를 얻고 있다는 의미 일 수 있습니까? –
여기에이 오래된 버그에 투표하십시오. https://connect.microsoft.com/VisualStudio/feedback/details/101545/incorrect-reference-to-base-class-copy-constructor-when-throwing-an-exception –