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

316 questions
129
votes
2 answers

Operator overloading : member function vs. non-member function?

I read that an overloaded operator declared as member function is asymmetric because it can have only one parameter and the other parameter passed automatically is the this pointer. So no standard exists to compare them. On the other hand,…
71
votes
15 answers

Where would you use a friend function vs. a static member function?

We make a non-member function a friend of a class when we want it to access that class's private members. This gives it the same access rights as a static member function would have. Both alternatives would give you a function that is not associated…
Swapna
  • 769
  • 2
  • 7
  • 8
33
votes
1 answer

How to make std::make_unique a friend of my class

I want to declare std::make_unique function as a friend of my class. The reason is that I want to declare my constructor protected and provide an alternative method of creating the object using unique_ptr. Here is a sample code: #include…
TheCrafter
  • 1,779
  • 1
  • 18
  • 39
30
votes
1 answer

C++ compile time counters, revisited

TL;DR Before you attempt to read this whole post, know that: a solution to the presented issue has been found by myself, but I'm still eager to know if the analysis is correct; I've packaged the solution into a fameta::counter class that solves a…
30
votes
4 answers

c++ error C2662 cannot convert 'this' pointer from 'const Type' to 'Type &'

I am trying to overload the c++ operator== but im getting some errors... error C2662: 'CombatEvent::getType' : cannot convert 'this' pointer from 'const CombatEvent' to 'CombatEvent &' this error is at this line if (lhs.getType() ==…
thiagoh
  • 6,285
  • 8
  • 44
  • 74
25
votes
2 answers

Declaring a namespace as a friend of a class

I was wondering if there is a way such that we make all functions defined within a specific namespace friend with a class? In particular, I have a class, for example: class C { private: // ... public: // ... friend…
kMaster
  • 1,915
  • 2
  • 18
  • 29
20
votes
3 answers

Difference between a pointer to a standalone and a friend function

I don't understand why the following does not compile (e.g. in gcc 9.10 or MS VS C++ 2019): class X { public: friend bool operator==(int, X const &); }; int main() { 2 == X(); // ok... static_cast(&operator==); …
Jarek C
  • 892
  • 5
  • 16
15
votes
2 answers

Can a friend of A be also a friend of A>?

Consider the following code: #include template class Container; template Container> make_double_container(const std::vector>&); template class Container { std::vector
Alberto Santini
  • 5,956
  • 1
  • 23
  • 36
14
votes
5 answers

Friend functions in C++

I have a doubt related to friend functions in C++. Friend function is not a member function of the claas and can be invoked directly from the main. So, what difference does it make if we keep the friend function within the private or the public part…
Kundan Kumar
  • 1,884
  • 7
  • 29
  • 53
12
votes
5 answers

How can I declare a friend function in a namespace that takes an inner class as a parameter?

Consider this code: namespace foo {} class A { class B { }; friend int foo::bar( B& ); }; namespace foo { int bar( A::B& ) { } } G++ 4.4.3 tells me: friendfun-innerclass.cpp:21: error: 'int foo::bar(A::B&)' should have …
12
votes
2 answers

clang/g++ difference with friend function

Why code below well compiled in g++ but get error on clang? #include class Object {}; class Print { public: template inline friend std::basic_ostream & operator<<(std::basic_ostream & out, const…
αλεχολυτ
  • 4,408
  • 1
  • 23
  • 62
11
votes
1 answer

Granting friendship to constructor/destructor of template class specialization - works under C++17, but fails under C++20

I have found a situation where code compiles successfully under C++17, but FAILS under C++20. This blocks us from upgrading our existing code design to the newer standard. Why doesn't this compile under C++20? It seems like a weird breach of…
Giffyguy
  • 17,946
  • 30
  • 81
  • 147
11
votes
1 answer

What is the point of the complicated scoping rules for friend declarations?

I recently discovered that friend declarations scoping follows extremely peculiar rules - if you have a friend declaration (definition) for a function or a class that is not already declared, it is automatically declared (defined) in the immediately…
Matteo Italia
  • 115,256
  • 16
  • 181
  • 279
11
votes
4 answers

friend vs member functions in Operator Overloading C++

Previously I learnt about overloading Operators in C++ as member functions as well as friend functions of a class. Although, I know how to overload operators in C++ using both techniques. But I am still confused that ** which one is better** ? A…
HN Learner
  • 494
  • 6
  • 19
11
votes
6 answers

C++ friend function hidden by class function?

Minimal example: class A { friend void swap(A& first, A& second) {} void swap(A& other) {} void call_swap(A& other) { swap(*this, other); } }; int main() { return 0; } g++ 4.7 says: friend.cpp: In member function ‘void…
Johannes
  • 2,537
  • 3
  • 22
  • 42
1
2 3
21 22