0

I want to implement a pure virtual function of a shared lib and call it in a lib-function. This lib-function will be called in the constructor of the lib. The class which contains the pure virtual function is in an own namespace. It looks like this:

//shared lib class:
namespace A{
namespace B{

class SharedLibClass : public object{
public:
          SharedLibClass(){init();}

protected:
          virtual const object* func()const=0;
private:
          void init(){const object* obj=func();}

}
}//namespace B
}//namespace A

//implementation class using the shared lib:
class B : public A::B::SharedLibClass
{
protected:
          virtual void func(){return this;}
}

This are my classes. I can compile them without problems, but when I run it, Qt prints out the following error:

pure virtual method called
terminate called without an active exception

I could imagine the problem is, that the parent class calls the virtual function before it is initialised or something like that. How can I solve this problem?

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
Paul
  • 233
  • 2
  • 3
  • 14
  • The signature of `func()` in your derived class doesn't match the signature of `func()` in `SharedLibClass`. Is this intentional? – RA. Jun 08 '13 at 18:29
  • Also, your question doesn't have much to do with qt specifically. It would probably be best to re-tag it "c++". – RA. Jun 08 '13 at 18:30
  • Duplicate of http://stackoverflow.com/questions/962132/calling-virtual-functions-inside-constructors – Oktalist Jun 08 '13 at 20:45

1 Answers1

0

You can't declare function void and then return something from it. And you need to declare your function with exactly the same signature if you want to make it implementation of existing pure virtual function:

class B : public A::B::SharedLibClass {
protected:
  virtual const object* func() const { /* your implementation */ }
}

Any good compiler recognizes these issues in compilation time. Check error reporting, maybe something is wrong in your build process (maybe your file isn't compiled at all?).

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127