CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Why does 'virtual' make a difference here ??

    Quote Originally Posted by John E View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured