Questions tagged [friend-function]

In C++ friend function is a kind of function that is a "friend" of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public.

Wiki

A friend function in a C++ class is defined outside the scope of a class but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend as follows

Example

// friend_functions.cpp
// compile with: /EHsc
#include <iostream>

using namespace std;
class Point
{
    friend void ChangePrivate( Point & );
public:
    Point( void ) : m_i(0) {}
    void PrintPrivate( void ){cout << m_i << endl; }

private:
    int m_i;
};

void ChangePrivate ( Point &i ) { i.m_i++; }

int main()
{
   Point sPoint;
   sPoint.PrintPrivate();
   ChangePrivate(sPoint);
   sPoint.PrintPrivate();
}

Tag usage

The tag can be used for programming related problems in implementation of friend functions, Theoretical questions such as "How to use friend functions" should be avoided

Read more

368 questions
3
votes
1 answer

accessing member in trailing return type vs. in requires clause of a locally defined friend function

Example code as below or on godbolt. All friend functions compile with gcc and Visual Studio. clang fails when trying to access S::foo() in trailing return type. If clang is correct, why is member access into incomplete type not allowed in…
3
votes
1 answer

How do we declare a friend function with a class template into .h file and define them into a .cpp file (not all in one header file)?

When separating the declaration/definition of (a friend function + a class template) an error occurs: error LNK2001: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class…
Lion King
  • 32,851
  • 25
  • 81
  • 143
3
votes
2 answers

"undefined reference to `operator>>(std::istream&, Complex&)" for template Complex

My code: #include using std::cin; using std::cout; using std::istream; using std::ostream; template class Complex { T real, img; public: Complex():real(0), img(0){} friend istream& operator>>(istream& input,…
Waliul
  • 35
  • 7
3
votes
3 answers

Redefinition error when defining friend function inside class template

I am learning friend declarations in C++ using the books listed here. So after reading, to test my understanding of the concept, i wrote the following program whose output i am unable to understand: template struct Name { friend…
Jason
  • 36,170
  • 5
  • 26
  • 60
3
votes
3 answers

Why Friend Function cannot access private members of a class

class A{ public: void printer(){ B obj; obj.private_data = 10; // <- fails, says member inaccessible } } class B{ friend void A::printer(); private: int private_data; } is it possible for printer function to access…
g0x0
  • 93
  • 7
3
votes
1 answer

Friend template operator

The following program does not compile in MS Visual Studio 19. #include #include template class A; template std::ostream &operator <<( std::ostream &, const A & ); template class…
3
votes
3 answers

Access protected data member via friend member function

EDIT: The original program has multiple files as shown in here I'm trying to access protected data member in my class from a friend member function of a different class. I keep getting an access error: 9:32: error: invalid use of incomplete type…
oded
  • 31
  • 2
3
votes
1 answer

Operator overloading on an enum nested in a class

The problem Given the following piece of code : template struct dummy { enum enumenum { a = 1, b = 2, c = 4 }; }; int main() { // I want this line to expands as : // dummy::enumenum a = operator~(dummy::a); …
3
votes
2 answers

Is it possible to have a non-friend function which can only be found by ADL?

C++ has a feature, that in-class-defined friend functions can only be found by ADL (argument dependent lookup): struct Foo { friend void fn(Foo) { } // fn can only be called by ADL, it won't be found by other lookup methods }; Is it possible to…
geza
  • 28,403
  • 6
  • 61
  • 135
3
votes
2 answers

Hidden friend templates in class template

The original working code I have a class template with two template parameters and an optimized operator== when the two types are the same and one another condition is satisfied. My original code is as follows (for demonstration purposes I make the…
Morwenn
  • 21,684
  • 12
  • 93
  • 152
3
votes
2 answers

define non-template function outside a templated class

I was learning about non-template friend function & template friend function to a templated class. So I tried the code below : #include template class cl { private : T val; public: cl()= default; explicit…
Blood-HaZaRd
  • 2,049
  • 2
  • 20
  • 43
3
votes
3 answers

How to fix previously-working injected template friend function?

I have recently updated gcc compiler from version 5 to 8, and it has broken our production code. A simplified version of the broken code is included below: #include // Imagine this has several template parameters not just Id and // this…
Andy G
  • 361
  • 1
  • 3
  • 11
3
votes
2 answers

How to declare template member function of template class X as friend of nested class X::Y

I have a template class. It has a template function. Both take different template parameters. There is an internal class that needs to make a friend of the enclosing class's template function. Compiler errors abound. The following toy example shows…
Loss Mentality
  • 364
  • 4
  • 11
3
votes
1 answer

C++ overloading: switching from friend to member function

I have this code which I wish to switch from friend functions to member functions: inline bool operator< (const MyClass& left, const MyClass& right) { return (((left.value == 1) ? 14 : left.value) < ((right.value == 1) ? 14 :…
J. Doe
  • 441
  • 1
  • 4
  • 13
3
votes
2 answers

Declare a template function taking two objects of a template class friend to (only) those two specializations

I have a template class Test (with an integer as template argument) and a template function (in this case operator*) that takes two objects of Test class with possibly different template arguments. The function needs to be friend to both its…
Azad
  • 1,050
  • 8
  • 24