vendredi 6 mai 2022

Calling a protected function from instance inside a derived class

I would like to call a protected function from an instance of a base class inside a derived class.

I have the following test code:

#include <iostream>

class baseA 
{
    protected:
    virtual bool foo()
    {
        std::cout <<"Base foo in action" << std::endl;
        return true;
    }
};

class derivedA
:public baseA
{
    public:
    baseA obj1_;

    virtual bool foo()
    {
        std::cout <<"I am going to call base foo" << std::endl;
        bool a = obj1_.foo(); // Can't be called
    }
};

int main(int argc, char** argv)
{
    derivedA obj; 
    obj.foo();

    return 0;
}

Currently is does not work because the function foois protected in the current context.

Is there a way to make it work while maintaining the protected specifier?

Best regards

Aucun commentaire:

Enregistrer un commentaire