views:

346

answers:

8

Where would you use a friend function vs a static function?

+1  A: 

A static function is a function that does not have access to this.

A friend function is a function that can access private members of the class.

tzenes
a static function has access to the internals of the class too
pm100
Only if they're static
tzenes
+5  A: 

Static functions are used when you want a function that is the same for every instance of a class. Such functions do not have access to "this" pointer and thus cannot access any non static fields. They are used often when you want a function that can be used without instantiating the class.

Friend functions are functions which are not in the class and you want to give them access to private members of your class.

And this(static vs. friend) is not a matter of using one vs the other since they are not opposites.

sklitzz
If you use a static function before instantiation, why associate it with the class? Could you give an example? Trying to get my head around this... Thanks!
Swapna
Lets say you have a class named Converter and all it does it converts units. You could have static functions such as Converter.ConvertFromMetersToMiles() , its logical to have those functions but you have no need to instantiate the class.
sklitzz
Your other functions can access your static data, so you could use a static function to set a static variable that controls some behavior of all of the instances of your class. There are obvious thread safety considerations in this kind of example, though.
thebretness
A: 

Here is what I think it is:

Friend function- when you need access to a different class member, but the classes are not related. Static function- when you no not need access to the 'this' pointer. But, I have a feeling there is more to it....

Swapna
+1  A: 

You would use a static function if the function has no need to read or modify the state of a specific instance of the class (meaning you don't need to modify the object in memory), or if you need to use a function pointer to a member function of a class. In this second instance, if you need to modify the state of the resident object, you would need to pass

this
in and use the local copy. In the first instance, such a situation may happen where the logic to perform a certain task is not reliant on an object's state, yet your logical grouping and encapsulation would have it be a member of a specific class.

You use a friend function or class when you have created code that is not a member of your class and should not be a member of your class, yet has a legitimate purpose for circumventing the private/protected encapsulation mechanisms. One purpose of this may be that you have two classes that have need of some common data yet to code the logic twice would be bad. Really, I have only used this functionality in maybe 1% of the classes I've ever coded. It is rarely needed.

San Jacinto
+2  A: 

Friend functions (and classes) can access the private and protected members of your class. There's rarely a good case for using a friend function or class. Avoid them in general.

Static functions may only access static data (that is, class-scoped data). They may be called without creating an instance of your class. Static functions are great for circumstances you want all of the instances of your class to behave the same way. You can use them:

  • as callback functions
  • to manipulate class-scoped members
  • to retrieve constant data that you don't want to enumerate in your header file
  • thebretness
    ^^This, i cam glad someone posted that they *generally* should be avoided !
    Craig
    A: 

    Static function can be used in many different ways.

    For example as simple factory function:

      class Abstract {
      private:
        // no explicit construction allowed
        Abstract(); 
        ~Abstract();
    
       public:
         static Abstract* Construct() { return new Abstract; }
         static void Destroy(Abstract* a) { delete a; }
       };
    
       ...
       A* a_instance = A::Conctruct();
       ...
       A::Destroy(a_instance);
    

    This is very simplified example but I hope it explains what I meant.

    Or as thread function working with Your class:

     class A {
    
     public:
        static void worker(void* p) {
                A* a = dynamic_cast<A*>(p);
                do something wit a;
        }   
     } 
    
     A a_instance;
     pthread_start(&thread_id, &A::worker, &a_instance);
     .... 
    

    Friend is completely different story and they usage is exactly as described by thebretness

    lollinus
    +2  A: 

    11.5 "c++ prgoramming language" by stroustrop

    ordinary member functions get 3 things

    1. access to internals of class
    2. are in the scope of the class
    3. must be invoked on an instance

    friends get only #1

    static functions get 1 and 2

    pm100
    +1  A: 

    The standard requires that operator = () [] and -> must be members, and class-specific
    operators new, new[], delete and delete[] must be static members. If the situation
    arises where we don't need the object of the class to invoke a function, then make
    the function static. For all other functions:
    if a function requires the operators = () [] and -> for stream I/O,
    or if it needs type conversions on its leftmost argument, or if it can be implemented using the class' public interface alone, make it nonmember ( and friend if needed in the first two cases)
    if it needs to behave virtually,
    add a virtual member function to provide the virtual behaviour
    and implement in terms of that
    else
    make it a member.

    Jagannath