4

I am getting an error that tells me

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

I am not sure how to begin solving this, I have never encountered this before.

in my .h I have:

public:
    virtual                         ~FXHost() throw();

in my .cpp I have:

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

Pointers appreciated.

ator
  • 272
  • 4
  • 11
  • 2
    Don't throw an exception from a destructor! http://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor – nos Aug 13 '10 at 00:31
  • 2
    your header declares the function as not throwing exceptions but your definition doesn't – AndersK Aug 13 '10 at 00:31
  • @nos I think that is what he is trying to say with his throw() – AndersK Aug 13 '10 at 00:33

3 Answers3

5

The throw() at the end of a function declaration is an exception specification. It means the function never throws an exception. This cannot be overridden (only restricted further) in derived classes, hence the error.

Since your implementation doesn't throw exceptions itself, all you need is adding throw() to your destructor declaration.

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

jpalecek
  • 47,058
  • 7
  • 102
  • 144
3
FXHost::~FXHost() throw()
{
   gHost = NULL;
}

Your implementation has to be at least as restrictive in terms of exception throwing as its declaration.

haffax
  • 5,898
  • 1
  • 32
  • 30
2

You want:

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

Though this destructor suggests a poor design - it's unlikely that a destructor will work correctly simply by setting a pointer, even a global pointer, to NULL.

  • Looks like resetting static state so that e.g. a singleton can be replaced after the old one is destroyed. Don't see a problem with that. All the real cleanup could be in member subjects which implement RAII. – Ben Voigt Aug 13 '10 at 00:48
  • @Ben So who is destroying the old one? and "subjects"? –  Aug 13 '10 at 00:51
  • I meant to type "subobjects". – Ben Voigt Aug 13 '10 at 02:46