mardi 28 février 2017

Why define a friend function inside class definition [duplicate]

This question already has an answer here:

In C++, we are allowed to define a friend function inside the class definition like:-

class A {
public:
    A(int a): mem(a){}
    ~A() {}
    friend void fun() {}
private:
    int mem;
};
void fun();

and then we can call this function, just like any regular function.

fun();

Can someone explain about( with examples ):-

1- In what cases do we need to define friend function inside the class definition.

2- What is special about this kind of definition which can not be achieved with just declaring function as friend in class and then defining the function outside.

Aucun commentaire:

Enregistrer un commentaire