Brandon
Brandon

Reputation: 23485

Call virtual parent protected function from within a Lambda

How can I call a virtual Parent function using a lambda when its protected?

template<typename T>
class Parent
{
    protected:
        virtual int Show()
        {
            std::cout<<"Parent::Show Called\n";
        }
};

class Child : Parent<Child>
{
    protected:
        virtual int Show() final override
        {
            std::cout<<"Child::Show Called\n";

            //supposed to be in a thread in my original code.
            auto call_parent = [&] {
                //Parent<Child>::Create(); //call other parent functions..
                Parent<Child>::Show();
            };

            call_parent();
        }
};

The error I get is:

error: 'int Parent::Show() [with T = Child]' is protected within this context

Any ideas? I'm using GCC/G++ 4.8.1 on Windows.

Upvotes: 2

Views: 1102

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153955

As a work-around you could call the Parent<Child>::Show() function via a trampoline:

class Child : Parent<Child>
{
    int trampoline() {
        return this->Parent<Child>::Show();
    }
protected:
    virtual int Show() final override
    {
        std::cout<<"Child::Show Called\n";
        auto call_parent = [&] {
            this->trampoline();
        };
        call_parent();
        return 0;
    }
};

Upvotes: 6

Related Questions