在c++中,protected修饰的成员属性和成员函数的访问权限:
(1)、本类中的成员函数(public/private/protected修饰的函数)
(2)、友元函数和友元类
(3)、派生类中的成员函数可以访问对应基类中的protected成员属性和成员函数。
#include <iostream>
#include <string>class Base {
public:Base() : m_data(0) {}protected:int m_data;private:void foo();
};class Derived : public Base {
public:Derived() = default;private:void print() {this->m_data = 2;}protected:void foo() {this->m_data = 2222;}
};int main () {Base *b = new Derived();b->m_data = 3; // 编译器报错,只能在类的成员函数中访问return 0;
}