Hi,
my linker is trying to look that up. So the implementation of a DTOR of an ABC can't be omitted even if the DTOR is pure virtual. Is that correct or do I suffer from some other fault?
Thank you in advance!
Hi,
my linker is trying to look that up. So the implementation of a DTOR of an ABC can't be omitted even if the DTOR is pure virtual. Is that correct or do I suffer from some other fault?
Thank you in advance!
Are you talking about Abstract Base Class? Then it should introduce an (empty) virtual destructor.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
If you think about sequence of destructing any object of the derived class, you will undestand - why.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
matsp:
Yes I mean abstract base class.
Hm strange, here is my code:
And here is my linker error:Code:// Model.h struct Model { virtual ~Model() = 0; virtual const std::vector<BlasVector> wireFrame(const BlasVector& heading) const = 0; }; // ModelFlyer.h class ModelFlyer : public Model { std::vector<BlasVector> mWireFrame; public: ModelFlyer(); virtual ~ModelFlyer(); virtual const std::vector<BlasVector> wireFrame(const BlasVector& heading) const; }; // ModelFlyer.cpp #include "ModelFlyer.h" ModelFlyer::ModelFlyer() :mWireFrame() {...} ModelFlyer::~ModelFlyer() {} const std::vector<BlasVector> ModelFlyer::wireFrame(const BlasVector& heading) const {...}
If I define Model::~Model(){} inside a Model.cpp the linker is pleased.1>ModelFlyer.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Model::~Model(void)" (??1Model@@UAE@XZ) referenced in function "public: virtual __thiscall ModelFlyer::~ModelFlyer(void)" (??1ModelFlyer@@UAE@XZ)
1>C:\workspace-eclipse\jschlegel-dev\Win32\Debug\bin\entityDebugger.exe : fatal error LNK1120: 1 unresolved externals
Do you know what I'm doing wrong?
vart:
What will I understand? Why the definition is needed or why "an (empty) virtual destructor is introduced [automatically]"? The first one is understood, the second doesn't seem to happen.
Last edited by pheres; 03-03-2008 at 07:27 AM.
The destructor must always be implemented, even if it's pure.
Code:class foo { public: virtual ~foo() = 0; }; inline foo::~foo() {}
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Ok, question answered. Thank you all!
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
It's called - the base destructor is always implicitly called in the derived destructor. Therefore it has to be implemented. To have it pure, it must be explicitly declared. Therefore the compiler won't provide the implementation.
End result: you have to implement it yourself.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
From the C++ Standard (2003) Section 12:Are you sure? If it is required to exist regardless, why would the compiler not implement one? Allowing you to forget to implement it seems kinda stupid.
I guess the idea is that if you declare the destructor and forget to implement it, something might be wrong, so instead of giving a default with an empty body, the compiler simply does nothing and lets the linker complain.The default constructor (12.1), copy constructor and copy assignment operator (12.8), and destructor (12.4) are special member functions. The implementation will implicitly declare these member functions for a class type when the program does not explicitly declare them, except as noted in 12.1.
EDIT:
I noticed that what I quoted does not talk about definition, so here are some snippets from Section 12.4:
From what I see, the former line talks about when an implicitly-declared destructor is defined, but does not talk about when an explicitly declared destructor is defined... so the implication is that explicitly declared destructors are to be explicitly defined. A virtual destructor can be implicitly defined too, when it belongs to a derived class whose base class' destructor is virtual.An implicitly-declared destructor is implicitly defined when it is used to destroy an object of its class type (3.7).
...
At the point of definition of a virtual destructor (including an implicit definition
Last edited by laserlight; 03-03-2008 at 12:36 PM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)