-
May 13th, 2017, 07:46 AM
#1
Why does 'virtual' make a difference here ??
I'm building a dll (let's call it DLL A) which uses a 3rd party dll (DLL B). But DLL A has never needed to link to the link library for DLL B. Recently, a new name() function got added to one of the classes in DLL B:-
Code:
class LIBCONTROLCP_API ControlProtocol : public PBD::Stateful, public PBD::ScopedConnectionList, public BasicUI
{
public:
ControlProtocol (Session&, std::string name);
virtual ~ControlProtocol();
std::string name() const { return _name; } // <--- This function got added
virtual int set_feedback (bool /*yn*/) { return 0; }
virtual bool get_feedback () const { return false; }
protected:
std::string _name;
};
(assume that LIBCONTROLCP_API is either __dllimport or __dllexport, as appropriate).
After the new function got added, I realised that I couldn't build DLL A any more without linking it to the link lib for DLL B. But the functions get_feedback() and set_feedback() have always worked without me needing the link lib. So I added virtual to name's declaration, like so:-
Code:
virtual std::string name() const { return _name; } // I declared it as virtual
and the linker seems to accept this now without needing DLL B's link lib any more. I'm just curious to know why virtual would make a difference here
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
May 14th, 2017, 01:32 AM
#2
Re: Why does 'virtual' make a difference here ??
Originally Posted by John E
I added virtual to name's declaration, like so:-
Code:
virtual std::string name() const { return _name; } // I declared it as virtual
and the linker seems to accept this now without needing DLL B's link lib any more. I'm just curious to know why virtual would make a difference here
I'm always amazed how things seem clearer after a good night's sleep! Presumably, virtual enforces late binding. Does that seem like the explanation?
"A problem well stated is a problem half solved.” - Charles F. Kettering
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|