开发者

Do access modifiers work for static class functions?

开发者 https://www.devze.com 2023-01-05 21:59 出处:网络
I just came across code that had protected static class functions, as in: class C开发者_如何学Python {

I just came across code that had protected static class functions, as in:

class C开发者_如何学Python {
...

    protected:
        static int fun() { ... }
};

I got curious if static class functions could have access modifiers and what would it mean? Since they are class globals and not pre-instance.

Thanks, Boda Cydo.


Access modifiers in C++ do not work per-instance. They always work per-class. That is how it's always been. Which makes it perfectly logical to have them apply to static members as well.

It is a rather popular misconception that access protection in C++ is somehow supposed to work per-instance, which seems to be what inspired your question as well.


It still serves the same purpose: Only derived classes can call that static function.


It means protected: static functions can be accessed from other member functions of that class or from the member functions of the derived classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消