Friend vs. Static

Started by
28 comments, last by fallenang3l 19 years, 6 months ago
I came to a point where I am going to have a member function that will be called from outside the class. I realized that there is no difference between the static and friend, except for the fact that friend is outside the scope of the class, and static is inside the scope of the class. Right now, I have the function implemented as a static function. Is there any reason to prefer one over the other?
I am the master of ideas.....If only I could write them down...
Advertisement
Static and friend are completely different.
maybe you could show a few lines of code or further explanation to demonstrate what you are trying to achieve. so far you don't make very much sense.
They are both ways of giving a function access to a class's private and protected members without it requiring an object instance to be called.

IMO static should be used for something that is part of the class, friend for something that only needs access. Yes, that's pretty vague :)
Quote:Original post by uavfun
They are both ways of giving a function access to a class's private and protected members without it requiring an object instance to be called.

No they're not. They're completely orthogonal—static and friend both provide access to private instance variables without having instances available in the same sense that skeleton keys and muffins both provide means of opening locked doors.

friend gives an outside entity access to protected and private members as though the outside entity (whether class or function) were part of the class. If the private (or protected) entities are not static, an instance is needed, or they don't exist. Instance variables live and die with the object.

static, which is completely orthogonal to friend, causes a variable or function to exist at class scope, independent of any instances. This does not negate any access modifiers; a private or protected static {variable, function} cannot be accessed by outside entities.
Make it a static member function if the functionality is tightly coupled with the concept of the class, preferably if the function does not have a parameter that is an instance of the class type. Having the function a static member logically portrays the functionality as tightly coupled to the class concept, as well as bring it in to the class scope.

Times you'd want the function to be a friend would be most notably when the function takes an instance of the class (or reference to an instance, etc) particularly when the function can logically be implemented to deal with other types. The reason why is primarily because of argument dependent lookup. Take for example a user-defined type which represents a numerical value. Now, you want to have an absolute value function called abs_val which takes a reference to a const instance of your class type and returns the instance's absolute value. This functionality can logically apply to other datatypes (such as the built in datatypes -- int, float, etc). As such, by making the function a friend instead of a static member function you can take advantage of function overloading as well as argument dependent lookup.

In code:

#include <iostream>namespace some_namespace{  class integer  {  public:    integer( int value_init ) : value_m( value_init ) {}  private:    int value_m;    friend int abs_val( integer source )    {      return source.value_m >= 0           ? source.value_m           : -source.value_m;    }  };}template< typename Type >Type abs_val( Type source ){  return source >= 0       ? source       : -source;}int main(){  float some_float = -3.5f;  ::some_namespace::integer some_integer = -6;  ::std::cout << "some_float absolute value: " << abs_val( some_float ) << ::std::endl              << "some_integer absolute value: " << abs_val( some_integer ) << ::std::endl;}


If you made the function a member, that functionality would not exist.
Quote:Original post by Miserable
static, which is completely orthogonal to friend, causes a variable or function to exist at class scope, independent of any instances. This does not negate any access modifiers; a private or protected static {variable, function} cannot be accessed by outside entities.


In the case of a public static member function (not variable) this works out to basically the same thing: you can call (albeit with slightly different syntax) the function without an instance of the object, and it has access to the internals of that class.

They do definitely have different purposes though.
Quote:Original post by uavfun
Quote:Original post by Miserable
static, which is completely orthogonal to friend, causes a variable or function to exist at class scope, independent of any instances. This does not negate any access modifiers; a private or protected static {variable, function} cannot be accessed by outside entities.


In the case of a public static member function (not variable) this works out to basically the same thing: you can call (albeit with slightly different syntax) the function without an instance of the object, and it has access to the internals of that class.

They do definitely have different purposes though.

What does friend have to do with this? friend is a way around access modifiers (to be used with caution, as it creates very strong coupling); it has nothing to do with whether a {variable,function} depends on an instance of a class or not.
Quote:Original post by Miserable
What does friend have to do with this? friend is a way around access modifiers (to be used with caution, as it creates very strong coupling); it has nothing to do with whether a {variable,function} depends on an instance of a class or not.

I think you're missing the point. The poster is asking when to use a friend function and when to use a static member function. Both a static member function and a function that is a friend of the class have the same exact same access rights and work generally the same way, only a static member function is in the class scope so it can't be used for overloading outside of that scope nor argument dependent lookup. Friend functions and static member functions are pretty much exactly the same aside from those points.
Quote:Original post by Polymorphic OOP
I think you're missing the point. The poster is asking when to use a friend function and when to use a static member function. Both a static member function and a function that is a friend of the class have the same exact same access rights and work generally the same way, only a static member function is in the class scope so it can't be used for overloading outside of that scope nor argument dependent lookup. Friend functions and static member functions are pretty much exactly the same aside from those points.

Ever have one of those days when your IQ suddenly drops to roughly that of a brick and you feel that suicide would be the ultimate expression of philanthropy? I just had another, reading a design question as though it were a matter of language mechanics; I apologise and shut up.

This topic is closed to new replies.

Advertisement