Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtual vs pure virtual base class functions and exporting from dll

I have a base class defined in a dll as below:

class Base
{
   public:
      virtual void doSomething(); // Definition in cpp
      virtual void doSomethingElse() = 0; // May have a definition in cpp
};

In another dll, I derive from Base and implement the necessary methods

class Derived : public Base
{
  public:
     // Use base implementation for doSomething
     void doSomethingElse() override;
}

I am getting linker error unresolved external symbol for Base::doSomething().

From what I understand, since the doSomething() was not overriden, the derived class needs access to the Base::doSomething definition which since I have not exported the Base class explicitly, is unavailable to derived which is in another module.

But why this problem doesn't happen with pure virtual function(it too could have a definition)?

P.S I am using VS2013

like image 530
Arun Avatar asked Oct 20 '25 04:10

Arun


1 Answers

But why this problem doesn't happen with pure virtual function(it too could have a definition)?

This would happen only if the pure virtual function of the base class is explicitly invoked. Otherwise, it is not necessary to be implemented.

E.g. Had you implemented Derived::doSomethingElse() as:

void Derived::doSomethingElse()
{
   // Do base class stuff first.
   Base::doSomethingElse();

   // Then do derived stuff
}

you would have seen the same problem for Base::doSomethingElse also.

like image 195
R Sahu Avatar answered Oct 21 '25 19:10

R Sahu