S
Samee Zahur
Question: How do friend functions and static member functions differ in
terms of functionality? I mean, neither necessarily needs an object of
the class to be created before they are called and either has access
only to static members of the class (ie. assuming no object of the
class is in scope - neither by arguments recieved nor by local
declarations). Any static member function like this:
//accessing static member i
static void cls::func(int funny){i=funny;}
would perform functions very similar to this function, assuming it
is a friend of cls:
//accessing static member i
void func(int funny){cls::i=funny;}
So I'd like to know more about the differences between the two uses.
Differences that I have come to think of:
statics can be hidden behind access priveleges, while anyone may call a
friend.
Statics access members with a slightly cleaner syntax
Many classes may grant friendship to the same function, not possible
with statics.
Friendship is not inherited to derived classes - statics are.
And probably (I'm not sure though) static members may become virtual,
supporting run-time polymorphism.
Now I just jotted these points down from five-minutes' thought, so it
would be nice if someone went through it to see if they are right, and
if some could be added to the list.
And another question ...
I've had numerous functions being declared static in my code while
their purpose really is being friends - reason: they needed to be
hidden by a protected privilege! This even happenned when my class
didn't even have a static member ... quite bewildering for someone
trying to read my codes! Can anyone tell me what is a nice way of
getting around this? (I have even hoped that one day static and friend
would become synonymous ... LOL)
Samee
Samee
terms of functionality? I mean, neither necessarily needs an object of
the class to be created before they are called and either has access
only to static members of the class (ie. assuming no object of the
class is in scope - neither by arguments recieved nor by local
declarations). Any static member function like this:
//accessing static member i
static void cls::func(int funny){i=funny;}
would perform functions very similar to this function, assuming it
is a friend of cls:
//accessing static member i
void func(int funny){cls::i=funny;}
So I'd like to know more about the differences between the two uses.
Differences that I have come to think of:
statics can be hidden behind access priveleges, while anyone may call a
friend.
Statics access members with a slightly cleaner syntax
Many classes may grant friendship to the same function, not possible
with statics.
Friendship is not inherited to derived classes - statics are.
And probably (I'm not sure though) static members may become virtual,
supporting run-time polymorphism.
Now I just jotted these points down from five-minutes' thought, so it
would be nice if someone went through it to see if they are right, and
if some could be added to the list.
And another question ...
I've had numerous functions being declared static in my code while
their purpose really is being friends - reason: they needed to be
hidden by a protected privilege! This even happenned when my class
didn't even have a static member ... quite bewildering for someone
trying to read my codes! Can anyone tell me what is a nice way of
getting around this? (I have even hoped that one day static and friend
would become synonymous ... LOL)
Samee
Samee