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

C++ friend function template overloading and SFINAE different behaviors in clang++, g++, vc++ (C++14 mode)

So, the following code builds and runs successfully under clang++ (3.8.0), but fails both under g++ (6.3.0) and vc++ (19.10.24903.0). Both g++ and vc++ complain about redefinition of operator&&. Does anyone know which compiler is at fault here. For…
3
votes
1 answer

Reduce compilation times and dependencies using friend class

In one of my projects I have a tree data structure, that might contain values of a generic type T. In order to reduce compilation times and dependencies I tried to move implementation details from the tree node class Node to class NodeImpl. The…
Aleph0
  • 5,816
  • 4
  • 29
  • 80
3
votes
2 answers

Member function of incomplete class as friend = formally valid?

The following code, where the nested class Info designates two member functions of the outer class Impl as friends, compiles nicely with Visual C++ and g++, with the code as given below. But, if the member functions aren't declared before Info, then…
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
3
votes
2 answers

Declare non-template friend function for template class outside the class

The following non-template code works well: struct A { }; struct B { B() {} B(const A&) {} friend B operator+(const B&) { return B(); } }; B operator+(const B&); int main() { A a; B b; +b; +a; } But if I make…
αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
3
votes
2 answers

Friend functions - Declaration Order

I have two classes called Screen and Window_mgr. Screen is allowing Window_mgr to modify its private / protected members via the friend function declaration. As a result Window_mgr defines at the very end of the code a non-member function called…
HansMusterWhatElse
  • 671
  • 1
  • 13
  • 34
3
votes
2 answers

Difference between friends defined in-class and outside: bug or feature?

Consider: struct Y { Y(float f) : f(f) {} float f; }; struct X { X(Y y) : i(y.f) {} int i; friend bool operator==(X x1, X x2) { return x1.i == x2.i; } }; int main() { return Y(1) == Y(2); // ERROR } This causes the following…
Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
3
votes
1 answer

Operator overloading in Fortran with no object creation such as in friend functions (C++)

I would like to know if there is anything similar to a "friend function", from C++, in Fortran. I would like to be able to make operator overloading without creating new objects since it is too expensive. I have already tried creating objects in a…
Bruno F
  • 65
  • 5
3
votes
2 answers

Friend function declaration/definition inside a namespace

Consider a class inside a namespace. The definition of the class declares a friend function. namespace Foo { class Bar { friend void baz(); }; } This should, based on what I know, declare baz() as a member of the innermost…
Avidan Borisov
  • 3,235
  • 4
  • 24
  • 27
2
votes
7 answers

Which C++ operators can not be overloaded without friend function?

Which C++ operators can not be overloaded at all without friend function?
user366312
  • 16,949
  • 65
  • 235
  • 452
2
votes
0 answers

"Satisfaction of atomic constraint depends on itself" error, but only on friend functions?

I am attempting to create a class that wraps another template-specified type, like a Box. I also want to add operators to enable comparing Box('s stored value) against any type that its stored value is comparable to. A requires clause is used to…
2
votes
0 answers

requires clause on non-template friend function of a template class

Reproducible code as below or on godbolt compiles with clang trunk and MSVC but fails with gcc trunk. Given that non-template friend function of a template class could be constrained by requires clause, is it a gcc bug here? template< typename…
wanghan02
  • 1,227
  • 7
  • 14
2
votes
1 answer

operator== compiles with msvc but not with gcc and clang

I am learning C++ using the books listed here. Now, to further check that I've understood the concepts I'm also writing simple sample programs. One such program that compiles with msvc but does not compile with clang and gcc is given below.…
2
votes
1 answer

"One or more multiply defined symbols found". Static and friend function

I'm trying to use interface classes and I have the following class structure: IBase.h: #pragma once class IBase { protected: virtual ~IBase() = default; public: virtual void Delete() = 0; IBase& operator=(const IBase&) =…
2
votes
1 answer

How to declare a concept for the member function and use it in the friend function

I came to the following example of using a concept, which checks if the member function is present in the class and then using this concept in the friend function. For example, to overload operator<< for a bunch of similar classes #include…
tupos
  • 150
  • 8
2
votes
1 answer

Friend function cannot access private members

When reading C++ Primer I encountered the following snippet (I've omitted code that I think is irrelevant): class Message { friend class Folder; public: // ... private: std::string contents; std::set folders; // ... }; //…
Flowerpot
  • 23
  • 4