+ 1
Protected data class members c++
Can the members of the protected class print out in the terminal? I’m quite confused how they work or how to access them
2 Réponses
+ 6
Class members with protected access specifiers are accessible by derived classes.
class Base {
private: int a {1};
protected: int b {2};
};
class Derived : public Base {
public:
void test() {
std::cout << a; // error
std::cout << b; // OK
}
};
int main() {
Derived obj;
obj.test();
}
+ 5
Yes they are accessible in the class and immediate subclasses.