pseudonym_127
pseudonym_127

Reputation: 357

Class member functions static and private

Does it make sense to have some class member function say f() both private and static? e.g.

class MyClass
{
 ...
private:

static int foo();
}

Thanks.

Hi thanks for all your comments! Ok, I got it. Yes, indeed initially I started using static functions in my class because they were not related to the objects of my class- were doing some other operations. Now I also realized they needn't be public. So, I think I'll leave them like this. I started to get some errors when I removed static keyword (apparently due to the way it was used throughout class, so I am lazy now to fix those, will just make it private and leave static :)).

Upvotes: 2

Views: 158

Answers (5)

4pie0
4pie0

Reputation: 29724

of course. You might use it for internal purposes of your class, in private functions i.e.

class A{
    public:
        static int i_;
private:
    void privatef(){hiden_i_++;}
    static int hiden_i_;
};

Upvotes: 0

user25064
user25064

Reputation: 2120

Absolutely! Imagine a class that is purely static such as a statistical functions class. Helper functions may be private and static.

Upvotes: 0

taocp
taocp

Reputation: 23634

Yes. For example: private static member functions used for initialization or other purposes.

See this post for more information: What is the use of private static member functions?

Upvotes: 5

Robᵩ
Robᵩ

Reputation: 168626

Yes, having a static private member function makes sense. It might, for example, be a stateless utility function used only by other members of the same class.

No, your class does not make sense. Since MyClass has no other members, no entity can ever see your MyClass::foo.

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

Sure, sometimes. For example, utility functions used by the public static methods of the same class.

Upvotes: 3

Related Questions