什么是访问说明符?
C++ 中的类/结构/联合有 3 个access specifiers。这些访问说明符定义了如何访问类的成员。当然,类的任何成员都可以在该类中访问(在同一类的任何成员函数内)。继续访问访问说明符的类型,它们是:
Public - 声明为 Public 的成员可以通过类的对象从类外部访问。
Protected - 声明为 Protected 的成员可以从类 BUT 外部访问,只能在从它派生的类中访问。
Private - 这些成员只能从类中访问。不允许外部访问。
源代码示例:
class MyClass
{
public:
int a;
protected:
int b;
private:
int c;
};
int main()
{
MyClass obj;
obj.a = 10; //Allowed
obj.b = 20; //Not Allowed, gives compiler error
obj.c = 30; //Not Allowed, gives compiler error
}
继承和访问说明符
C++ 中的继承可以是以下类型之一:
-
Private继承
-
Public继承
-
Protected继承
以下是与这些相关的成员访问规则:
第一个也是最重要的规则 Private 一个类的成员除了同一个类的成员之外,永远不能从任何地方访问。
公有继承:
基类的所有Public成员都成为派生类的Public成员&
基类的所有Protected 成员都成为派生类的Protected 成员。
即成员的访问权限没有变化。我们之前讨论的访问规则会进一步应用于这些成员。
代码示例:
Class Base
{
public:
int a;
protected:
int b;
private:
int c;
};
class Derived:public Base
{
void doSomething()
{
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};
int main()
{
Derived obj;
obj.a = 10; //Allowed
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}
私有继承:
基类的所有Public成员变为派生类的Private成员&
基类的所有Protected 成员都成为派生类的Private 成员。
代码示例:
Class Base
{
public:
int a;
protected:
int b;
private:
int c;
};
class Derived:private Base //Not mentioning private is OK because for classes it defaults to private
{
void doSomething()
{
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};
class Derived2:public Derived
{
void doSomethingMore()
{
a = 10; //Not Allowed, Compiler Error, a is private member of Derived now
b = 20; //Not Allowed, Compiler Error, b is private member of Derived now
c = 30; //Not Allowed, Compiler Error
}
};
int main()
{
Derived obj;
obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}
受保护的继承:
基类的所有Public成员都成为派生类的Protected成员&
基类的所有Protected 成员都成为派生类的Protected 成员。
代码示例:
Class Base
{
public:
int a;
protected:
int b;
private:
int c;
};
class Derived:protected Base
{
void doSomething()
{
a = 10; //Allowed
b = 20; //Allowed
c = 30; //Not Allowed, Compiler Error
}
};
class Derived2:public Derived
{
void doSomethingMore()
{
a = 10; //Allowed, a is protected member inside Derived & Derived2 is public derivation from Derived, a is now protected member of Derived2
b = 20; //Allowed, b is protected member inside Derived & Derived2 is public derivation from Derived, b is now protected member of Derived2
c = 30; //Not Allowed, Compiler Error
}
};
int main()
{
Derived obj;
obj.a = 10; //Not Allowed, Compiler Error
obj.b = 20; //Not Allowed, Compiler Error
obj.c = 30; //Not Allowed, Compiler Error
}
请记住,相同的访问规则适用于继承层次结构中的类和成员。
注意事项:
- 访问规范是每个类而不是每个对象
请注意,访问规范 C++ 是基于每个类而不是基于每个对象的。
一个很好的例子是,在复制构造函数或复制赋值运算符函数中,可以访问正在传递的对象的所有成员。
- 派生类只能访问其自身基类的成员
考虑 following code example:
class Myclass
{
protected:
int x;
};
class derived : public Myclass
{
public:
void f( Myclass& obj )
{
obj.x = 5;
}
};
int main()
{
return 0;
}
它给出了一个编译错误:
prog.cpp:4: 错误:'int Myclass::x' 受保护
因为派生类只能访问其自己的基类的成员。请注意,此处传递的对象obj 与正在访问它的derived 类函数没有任何关系,它是一个完全不同的对象,因此derived 成员函数无法访问其成员。
什么是friend? friend 如何影响访问规范规则?
您可以将一个函数或类声明为另一个类的friend。当您这样做时,访问规范规则不适用于friended 类/函数。类或函数可以访问该特定类的所有成员。
friends 也会破坏封装吗?
不,它们没有,相反,它们增强了封装!
friendship 用于表示两个实体之间有意的强耦合。
如果两个实体之间存在特殊关系,即一个实体需要访问其他 private 或 protected 成员,但您不希望 everyone 使用public 访问说明符,那么您应该使用 friendship。