views:

44

answers:

3

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

class C {
...

    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.

A: 

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

Shirik
Oh? I thought static class functions always were accessible, no matter what.
bodacydo
static just means they're accessible without an instance of the object available. They have effectively global scope, but they still obey access restrictions.
Cogwheel - Matthew Orlando
A: 

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

vpit3833
+1  A: 

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.

AndreyT
Thank you for explaining this. Believe or not, I thought they were `per-instance`! A very serious mistake in my learning process. Now I know that they are `per-class`.
bodacydo