Re: COMPILE!
From: tom_usenet (tom_usenet_at_hotmail.com)
Date: 08/05/04
- Next message: Marco Manfredini: "Re: Object factory and Intel compiler"
- Previous message: Snyke: "Design structure for a MMORPG server core written in C++"
- In reply to: JKop: "COMPILE!"
- Next in thread: JKop: "Re: COMPILE!"
- Reply: JKop: "Re: COMPILE!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 05 Aug 2004 17:56:49 +0100
On Thu, 05 Aug 2004 15:17:26 GMT, JKop <NULL@NULL.NULL> wrote:
>g++ cnt.cpp -ansi -pedantic -o c.exe
>
>C:\WINDOWS\TEMP/ccgrbaaa.o(.text$_ZN15AutoDestructiveIiED2Ev+0x16):cnt.cpp:
>undefined reference to `AutoDestructive<int>::CleanUp()'
>
>
>Can some-one else please try compile it and see what happens.
>
>Anyone know what's wrong with the code?
>
>
>Here's the code:
>
>
>typedef int HKEY;
>const int ERROR_SUCCESS(5);
>
>void RegCloseKey(HKEY hkey) {}
>int RegOpenKeyEx(HKEY *hkey) { return ERROR_SUCCESS; }
>
>template<class T>
>class AutoDestructive
>{
>public:
>
> T t;
> bool to_be_auto_destructed;
>
> AutoDestructive() : t(), to_be_auto_destructed(false) {}
>
> virtual void CleanUp() = 0;
>
> ~AutoDestructive()
> {
> this->CleanUp();
There's your problem - calling a virtual function from a destructor
doesn't do what you think it does. Calling a pure virtual function
from a destructor causes undefined behaviour (and fortunately VC seems
to indicate this with a linker error, which is as nice as undefined
behaviour gets).
Basically, destruction goes for typical implementations:
derived class destructor called
derived members destructed in reverse order of declaration
*vtable pointer updated to point to base class vtable*
base class destructor called
base class members destructed in reverse order of declaration
The emphasised line is what causes the problem for you - calling a
virtual function from a constructor or destructor calls the function
in the base object currently being constructed or destructed, not the
one in the most derived class.
Tom
- Next message: Marco Manfredini: "Re: Object factory and Intel compiler"
- Previous message: Snyke: "Design structure for a MMORPG server core written in C++"
- In reply to: JKop: "COMPILE!"
- Next in thread: JKop: "Re: COMPILE!"
- Reply: JKop: "Re: COMPILE!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|