TMOTTM
TMOTTM

Reputation: 3381

Why is the inherited function inaccessible?

A small example to reproduce the error in Visual Studio (Community) 2022:

#include <iostream>
#include <memory>

class Base {
public:
    virtual void b();
protected:
    std::string Method() {
        return "Base::Method";
    }
};

class SubA : public Base {
public:
    // Prevent compiler from optimizing class away.
    int doSomething() {
        return 7;
    }
};


int main() {
    SubA sa;
    sa.doSomething();
    sa.Method(); // <-- Function is inaccessible.
}

Why is the error? The inheritance is public and I'm calling Method on an object from the SubA class.

Upvotes: 0

Views: 57

Answers (1)

eerorika
eerorika

Reputation: 238341

The inheritance is public

But the member function is protected. You're accessing the member function from main which is outside of the scope of the class or its decendants, so it has no access to protected member functions.

Upvotes: 2

Related Questions