Note that there are some explanatory texts on larger screens.

plurals
  1. POWhere should the pure virtual destructor be declared?
    primarykey
    data
    text
    <p>EDIT: Apparently the question is not clearly formulated enough. The issue I am having is that when the destructor is defined in the header it gets added into multiple .obj files and the linker complains. The actual question is:</p> <p>When I add the destructor to a CPP file in a DLL project and use the dll with dynamic loading and the interface header file, does the base destructor still get called to prevent leaking memory?</p> <p>I am using MSVC 10.0 and have a DLL project that implements an interface. The interface is an abstract (pure virtual) base class. The idea is that the header is used with dynamic loading of the library. Therefore, I have used a pure virtual destructor to make sure the destructor in the base class gets called. Here is sample code to explain this:</p> <pre><code>//ISplitter.h #pragma once struct param { int something; } class ISplitter { public: virtual ~ISplitter() = 0; virtual void useful() = 0; } ISplitter::~ISplitter() { /* Make sure base class destructor gets called */ } </code></pre> <p>And the main implementation header</p> <pre><code>//CSplitter.h #pragma once #include "CHelper.h" #include "ISplitter.h" class CSplitter : public ISplitter { private: CHelper hlp; public: ~CSplitter(); void useful(); } </code></pre> <p>Some helper class</p> <pre><code>//CHelper.h #pragma once #include "ISplitter.h" // I need the struct // Class definition should go here but is irrelevant </code></pre> <p>Now the problem is that the linker generates an error that tells me the destructor: ISplitter::~ISplitter(void) has been multiply declared and the system will not build. Error:</p> <pre><code>CHelper.obj : error LNK2005: "public: virtual __cdecl ISplitter::~ISplitter(void)" (??1ISplitter@@UEAA@XZ) already defined in CSplitter.obj </code></pre> <p>What is the correct way to fix this? I have placed the destructor in ISplitter.cpp, but I am worried this may not work if I dynamically load the library and upcast the base class to ISplitter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload