Access Friend Function Defined in Class

Access friend function defined in class

class A{

public:
friend void fun(A a){std::cout << "Im here" << std::endl;}
friend void fun2(){ std::cout << "Im here2" << std::endl; }
friend void fun3();
};

Although your definition of fun2 does define a "global" function rather than a member, and makes it a friend of A at the same time, you are still missing a declaration of the same function in the global scope itself.

That means that no code in that scope has any idea that fun2 exists.

The same problem occurs for fun, except that Argument-Dependent Lookup can take over and find the function, because there is an argument of type A.

I recommend instead defining your functions in the usual manner:

class A {
friend void fun(A a);
friend void fun2();
friend void fun3();
};

void fun(A a) { std::cout << "I'm here" << std::endl; }
void fun2() { std::cout << "I'm here2" << std::endl; }
void fun3();

Notice now that everything works (except fun3 because I never defined it).

How to access friend function from another class?

The other answers tell you how to solve the problem in the easiest (and probably the best) way. However, if you really want to only grant the friend privilege to single member function, more work is required:

class class1;

// To befriend a member function of class2, it must be defined before the friendship declaration
class class2{
public:
int getter(class1 o1);
};


class class1{
int var;
public:
void setter(int);
//correct syntax mentions the class name
friend int class2::getter(class1 o1);
};

void class1::setter(int v)
{
var =v;
}

// definition of this function must be after class1 definition,
// so it can know that `class1` has member `var`
// therefore it cannot be defined inline in class
int class2::getter(class1 o1)
{
return o1.var;
}

See it online

Why define a friend function inside class definition

Assuming that you already know what is a friend function, there is absolutely no special meaning to your example: what you have is a regular friend function, with its declaration and definition combined.

Recall that friendship needs to be declared inside the class that "friends" a function. After that, the function can be defined at some place, for which you have two choices:

  • Outside the class - that is the common way of defining a friend function, or
  • Inside the class - that is what your example has.

Basic considerations for going with one approach vs. the other are the same as the rules that you use to decide between defining a member-function inside or outside the class.

Why Friend Function cannot access private members of a class

Why Friend Function cannot access private members of a class?

They can, but you may need to split the definition of the class up a bit.

Imaginary files added:

Define A (file a.hpp):

class A {
public:
void printer();
};

Define B (file b.hpp):

#include "a.hpp" // B must see the definition of A to befriend a member function

class B {
friend void A::printer();

private:
int private_data;
};

Define A's member function (file a.cpp):

void A::printer() {
B obj;

obj.private_data = 10;
}

C++ friend operator definition inside class body serves as function declaration?

Even if we define the function inside the class, we must still provide a declaration outside of the class itself to make that function visible. A declaration must exist even if we only call the friend from members of the friendship granting class. This means in your example, you should forward declare the function f as shown below:

//forward declare f
void f();

struct X
{
//no ADL here since this function f has no parameters of type X. So we also need to declare this outside struct X which we did by providing forward declaration
friend void f()
{

}

void foo()
{
f(); // WORKS NOW because of the forward declaration
X tmp = X {1} + X {2}; //operator+ found using ADL
}
//other members here
};

The output of the program can be seen here.

The reason why the call to operator+ works is because of ADL which stands for argument dependent lookup. In particular, operator+ can be found using ADL since it has parameters of type X. So in this case, you do not need to explicitly declare operator+ outside struct X.



Related Topics



Leave a reply



Submit