Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 287
Dixit Singla Avatar asked Jul 30 '13 08:07

Dixit Singla


1 Answers

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
}
like image 195
juanchopanza Avatar answered Oct 11 '22 23:10

juanchopanza