Dixit Singla
Dixit Singla

Reputation: 2620

Can we declare a friend function with no argument?

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();

Upvotes: 8

Views: 5386

Answers (9)

bkausbk
bkausbk

Reputation: 2790

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;
    }
};

Upvotes: 0

A. H.
A. H.

Reputation: 952

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;
}

Upvotes: 6

Asha
Asha

Reputation: 11232

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.

Upvotes: 5

Stefano Falasca
Stefano Falasca

Reputation: 9097

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), ...)

Upvotes: 1

Karadur
Karadur

Reputation: 1246

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.

Upvotes: 0

Shumail
Shumail

Reputation: 3143

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.
}

Upvotes: 2

Raju
Raju

Reputation: 1169

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

Upvotes: 1

Oleksiy
Oleksiy

Reputation: 39840

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

Upvotes: 3

juanchopanza
juanchopanza

Reputation: 227438

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
}

Upvotes: 16

Related Questions