简体   繁体   English

访问说明符和虚函数

[英]Access specifiers and virtual functions

What are the rules for accessibility when virtual functions are declared under 3 different access specifiers specified by C++(public, private, protected) What is the significance of each? 当在C ++指定的3个不同访问说明符(公共,私有,受保护)下声明虚函数时,可访问性的规则是什么?每个的重要性是什么? Any simple code examples to explain the concept will be highly useful. 任何解释这个概念的简单代码示例都非常有用。

Access specifiers apply in the same way as they would to any other name during name lookup. 访问说明符的应用方式与在名称查找期间对任何其他名称的应用方式相同。 The fact that the function is virtual does not matter at all. 功能是虚拟的这一事实根本不重要。

There is a common mistake which sometimes happens with respect to virtual functions. 有时会出现与虚函数有关的常见错误。

If name lookup determines a viable function to be a virtual function, the access specifier of the virtual function is checked in the scope of the static type of the object expression used to name the function. 如果名称查找将可行函数确定为虚函数,则在用于命名函数的对象表达式的静态类型的范围内检查虚函数的访问说明符。 At run time, the actual function to be called could be defined in the derived class with a completely different access specifier. 在运行时,可以使用完全不同的访问说明符在派生类中定义要调用的实际函数。 This is because 'access specifiers' are a compile time phenomonon. 这是因为“访问说明符”是编译时的现象。

// Brain compiled code ahead
struct A{
   virtual void f() {}
private:
   virtual void g() {}
protected:
   virtual void h() {}
};

struct B : A{
private:
   virtual void f() {}           // Allowed, but not a good habit I guess!
};

B b;
A &ra = b;

ra.f();    // name lookup of 'f' is done in 'A' and found to be public. Compilation 
           // succeeds and the call is dynamically bound
           // At run time the actual function to be called is 'B::f' which could be private, protected etc but that does not matter

Virtual functions are just like regular functions (exception of pure virtuals) when they are used in the base class. 当在基类中使用虚函数时,它们就像常规函数(纯虚函数的例外)。

To summarise from the top of my head: 总结一下我的头脑:

public functions can be accessed by anyone. 任何人都可以访问公共功能。 private functions can be accessed only be the class and its friends protected functions are like private ones, only they can be accessed by derived classes. 私有函数只能是类访问,其朋友受保护的函数就像私有函数一样,只有派生类才能访问它们。

Public is the interface, and private/protected functions are the internals. Public是接口,private / protected函数是内部。 Also note that all the local variables (according to encapsulism) should be protected/private. 另请注意,所有局部变量(根据封装)都应该受到保护/私有。

Now, when it comes to derived classes, you derive a class like this: 现在,当涉及派生类时,您派生出一个这样的类:

class A : [public | protected | private] B
{
};

Now, the public/private/protected qualifier infront of B states the least restrictive security level to inherit from the base class. 现在,B的公共/私有/受保护限定符规定了从基类继承的限制最少的安全级别。 This is not a "filter" for the methods and local variables in the sense that some aren't inherited, it just changes their security level to the one specified if they are less restricted (more public). 这不是方法和局部变量的“过滤器”,因为某些方法和局部变量没有被继承,只是如果它们受到较少限制(更公开),它只会将其安全级别更改为指定的安全级别。

So class A : public B will leave the inherited base members as they are while, class A : private B will change them all to private members. 所以class A : public B将保留继承的基础成员,而class A : private B将把它们全部更改为私有成员。

Hope this makes sense to you and answers your question. 希望这对你有意义并回答你的问题。 If not, tell me! 如果没有,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM