Possible Duplicates:
Where would you use a friend function vs a static function?
C++: static member functions
When is it appropriate to use a static member function in C++? Please give me a real world example.
Good uses of static member functions:
Note that the last case applies to a protected static member function but not a private one. In the latter case you would just put it into the compilation unit of the class, hiding it away as an implementation detail. For a protected one though you want it to be visible, albeit in a restricted way.
A typical case of that one is "cheating" the lack of inheritance of friendship.
class B
{
friend class A;
// lots of private stuff
};
class A
{
protected:
static void callsSomePrivateMembers( B& b );
};
class AChild : public A
{
void foo( B& b );
}
void AChild::foo( B& b )
{
// AChild does not have private access to B as friendship is not inherited
// but I do have access to protected members of A including the static ones
callsSomePrivateMembers( b ); // get to call them through a back-door
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With