2010-08-13 53 views
4

我正在告诉我错误 - “抛出不同的异常”,在C++

error: declaration of 'virtual FXHost::~FXHost()' throws different exceptions 
error: than previous declaration 'virtual FXHost::~FXHost() throw()' 

我不知道如何开始解决这个错误,我以前从来没有遇到过这种。

在我的.h

我:

public: 
    virtual       ~FXHost() throw(); 

在我的.cpp我:

FXHost::~FXHost() 
{ 
    gHost = NULL; 
} 

指针赞赏。

+2

不要从析构函数中抛出异常! http://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor – nos 2010-08-13 00:31:06

+2

你的头声明函数不抛出异常,但你的定义不 – 2010-08-13 00:31:50

+0

@nos我认为这就是他正在尝试用他的投掷() – 2010-08-13 00:33:25

回答

5

函数声明结尾的throw()是一个异常规范。这意味着函数永远不会抛出异常。这不能在派生类中被覆盖(仅限于进一步限制),因此是错误。

由于您的实现本身不会抛出异常,所有您需要的是将throw()添加到您的析构函数声明中。

See here why you should (not) use this (mis)feature of C++

2

你想:

FXHost::~FXHost() throw() 
{ 
    gHost = NULL; 
} 

虽然这个析构函数提出了一种糟糕的设计 - 这是不可能的析构函数将通过设置一个指针,即使是一个全球性的指针,为NULL正常工作简单。

+0

看起来像重置静态,以便例如一个老人被毁后可以更换一个单身人士。没有看到这个问题。所有真正的清理可能在实施RAII的成员主题中。 – 2010-08-13 00:48:58

+0

@Ben那么谁在摧毁旧的?和“科目”? – 2010-08-13 00:51:18

+0

我打算输入“子对象”。 – 2010-08-13 02:46:29

3
FXHost::~FXHost() throw() 
{ 
    gHost = NULL; 
} 

您的实现必须至少在异常抛出其声明的条款限制。