Can we declare a friend function with no argument?
Asked Answered
F

9

8

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();
Fagaceous answered 30/7, 2013 at 8:22 Comment(4)
@sehe: Why exactly? There are conformist friends also.Hargrove
@Nawaz here let me explainDedifferentiation
@P0W What do you mean by "will it make sense"?Before
@MarkGarcia I mean why would one create object of class and then access private members in that way.Gladdy
D
6

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;
}
Dedifferentiation answered 30/7, 2013 at 8:28 Comment(2)
Please give an example?Fagaceous
@DixitSingla added examplesDedifferentiation
V
16

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
}
Veracruz answered 30/7, 2013 at 8:24 Comment(0)
D
6

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;
}
Dedifferentiation answered 30/7, 2013 at 8:28 Comment(2)
Please give an example?Fagaceous
@DixitSingla added examplesDedifferentiation
D
5

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.

Dwelt answered 30/7, 2013 at 8:26 Comment(5)
Thanks Asha for your time Please tell me one thing more can we define friend function inside the class? If no then why not?Fagaceous
@juanchopanza: in this particular case, it seems its not possible. Compiler will not be able to find fun when it is not taking any arguments. See here: codepad.org/6aaNmHK6Dwelt
@Dwelt Confused that why compiler will no be able to find fun when it is not taking any arguments? please elaborate?Fagaceous
@DixitSingla: It is because of the way in which Argument Dependent Lookup works in C++. Take a look at this question for more details: #7786386Dwelt
@Dwelt Argument dependent lookup, What does it mean?Fagaceous
P
3

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

Padegs answered 30/7, 2013 at 8:26 Comment(0)
C
2

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.
}
Cambodia answered 30/7, 2013 at 8:24 Comment(0)
J
1

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

Jessee answered 30/7, 2013 at 8:26 Comment(0)
S
1

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

Seniority answered 30/7, 2013 at 8:28 Comment(2)
Can you please Explain with an example?Fagaceous
You can use it as a helper function. One scenario may be to convey some information when an object state changed, you can call the friend function which will display some text.Seniority
T
0
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.

Taiga answered 30/7, 2013 at 8:34 Comment(0)
P
0

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;
    }
};
Podite answered 30/7, 2013 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.