0

I am not able to call a pure virtual function of the base class within a lambda of the inherited class. I simplified the problem to the following easy example...

The usecase is, that the class Test should again be a base class of some other class, that will implement the pure virtual function (anywhere in the program).

class Base
{
    public:

        virtual void DoSomething(Parameter* p) = 0;

};

class Test : public Base 
{
    public:

        void addFunction() 
        {
            mFunction = [this] (Parameter* p) { Base::DoSomething(p); };
        }

    private:

    std::function<void(Parameter* p)> mFunction;
};

I get following compilation error:

undefined reference to Base::DoSomething(Parameter* p)

My current solution is to call a tempFunction in the base class, and then call the virtual function. But this seems more like a hack... and means unnecessary overhead:

class Base
{
    public:

        virtual void DoSomething(Parameter* p) = 0;

    protected:

        void tempDoSomething(Parameter* p) { DoSomething(p); }  // call this function instead

};

I am using Android-NDK with clang compiler.

kaiser
  • 940
  • 1
  • 10
  • 25
  • 1
    If you want to call the base function, you probably don't want to make it pure. – O'Neil May 12 '18 at 00:59
  • In any case you'll have to provide a body. [Use-cases of pure virtual functions with body?](https://stackoverflow.com/q/2609299/3893262). – O'Neil May 12 '18 at 01:01
  • 1
    don't use `Base:: `, that explicitly causs ill-formed program because you call absent (pure, abstract) function member of class Base – Swift - Friday Pie May 12 '18 at 01:01
  • I need to call `Base::` because in my real world problem i have many templated bases, all implementing the same func,with different params. Implicit conversion doesn't work. I will try to provide a body to the pure virtual... PS: never heard of a body of a pure virtual method. Thx – kaiser May 12 '18 at 12:21
  • A body of the virtual function does not work. With body the emtpy implementation is called, and not the virtual function of the inheriting class... Seems that i am using the only solution right now. – kaiser May 13 '18 at 01:13

0 Answers0