Question

Is it possible?

class sample {
        private:
           int x;
        public:
           friend void fun();
};

friend function with no argument!

In my opinion not possible

Because friend functions are not "member" of classes So we can not call with class object

like:

sample s;
s.fun();
Was it helpful?

Solution 2

yes you can. There may be many reasons for that such as access to private static members or there might be a global instance of sample. It is also possible that fun creates an instance of sample and grab its privates.

working example for function creating instance and doing stuff with it :

#include <iostream>
class sample {
    private:
       int x;
    public:
       friend void fun();
};

void fun()
{
    sample s;
    s.x = 555555555;
    std::cout << s.x;
}
int main(){
    fun();
    return 0;
}

example with global instance:

#include <iostream>
#include <string>
class sample {
    private:
       int x;
    public:
       friend void fun();
};
sample s;

void fun()
{

    s.x = 555555555;
    std::cout << s.x;
}
int main(){
    fun();
    return 0;
}

example with private static member :

#include <iostream>
#include <string>
class sample {
    private:
       int x;
       static const int w = 55;
    public:
       friend void fun();
};


void fun()
{

    std::cout << sample::w;
}
int main(){
    fun();
    return 0;
}

OTHER TIPS

Yes, you can:

void fun()
{
  sample s;
  s.x++;   // OK, fun() is a friend of sample
}

or

sample globalSample; // yikes, a global variable

void fun()
{
  int i = globalSample.x; // OK, fun() is a friend of sample
}

Of course you can.. see here for sample code. But to define function inline you need to take the sample as parameter, otherwise ADL will not work and compiler will not be able to resolve func. See sample here.

It is possible to have friend functions without arguments. You need another way of getting access to an object of class sample. Don't forget, friend functions also allow you access private static variables of class sample, in case you need that

Yes but then for variables, you will need them to be global then. Global object of type sample in your case. Or create object in function internally in definition of fun() probably.

sample globalObject;

void fun()
{
 globalObject.x++;   //this will work as fun() is friend function of sample.
}

It is possible, to have a friend function with no arguments. It Seldom used.

Of course it is possible. It seems you are wondering why one would do that. For example, it can access a static private member variable. Or it can access private members of objects it obtains by some means (singleton, global (container of) object(s), ...)

sample s;
s.fun();

This will not work anyway because 'friendship' in C++ is just lifting access restrictions to private/protected members. A friend is not a class member so you're not able to call s.fun().

However you can declare a friend function like you described, and from that function you will have access to private members of sample class.

A friend method may be usefull because with this access to private/protected data members are restricted to only those method specified. Wether this method has arguments or return types doesn't matter.

template <class T>
class CLinkedListNode {
public:
    CLinkedListNode(CLinkedList<T>* Parent) : _Parent(Parent) {
    }
protected:
    CLinkedListNode * _Next;
    T _Data;
    CLinkedList<T>* _Parent;

    friend CLinkedListNode<T>* CLinkedList<T>::find(); // Only CLinkedList<T>::find() may access data members
    friend void sort(); // All sort methods may access data members
};

So imagine there is a class CLinkedList whose find() method is able to access all data members of CLinkedListNode.

template <class T>
class CLinkedList {
// ...
    CLinkedListNode<T>* find() {
        CLinkedListNode<T>* Node = new CLinkedListNode<T>(this);
        CLinkedListNode<T>* Find = Node->_Next;
        while (Find->_Next) {
            // Do something
            Find = Find->_Next;
        }
        // ...
        return Node;
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top