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
2
votes
0 answers

Multiple operator override behaviors by manipulating type

So let's say I'm overwriting the inequality checks of a structure describing a point. I have it set to compare the magnitude by default. I want to have an option to check each value instead, and I was thinking of ways to do it inline. I could make a…
Friendnard
  • 21
  • 3
2
votes
2 answers

Error for default parameter value of non-member template friend function

Why does the following code compile with GCC but not with Clang? Who is right and why? class TF { private: struct S { }; template friend void F(T x, S s, int v =…
2
votes
0 answers

Why do we need the friend keyword in C++?

I was reading about friend keyword in c++ and couldn't understand why we need such a thing. For example, I was told that since the following function: bool operator==(const Rational& r1, const Rational&r2); needs to access private members like…
user13611620
2
votes
2 answers

How to enable a friend class's friend function access its private members directly in C++

I'm writing a sparse matrix class, and I want to output the sparse matrix by overloading operator<<. I'm wondering how to enable a friend function of SMatrix (operator<<) directly (not by some interface) access private data members of TriTuple?…
2
votes
2 answers

Are non-template friends of template classes instantiated?

Consider the following code : //Allows to automatically define post in/de-crement operators from their pre- version template struct Arithmetic { //Not a template? friend constexpr auto operator++(T& l, int) { auto old = l;…
2
votes
4 answers

Bilateral friend functions in C++: how to make it compile?

I have the following C++ files: A.h #ifndef A_H #define A_H #include "B.h" class A { private: int m_a; public: A(); void a(const B &b); friend void B::b(const A &a); }; #endif // A_H A.cpp #include…
artaxerxe
  • 6,281
  • 21
  • 68
  • 106
2
votes
1 answer

Friend function - member is inaccessible

I'm taking a C++ course, and I came across something I can't wrap my head around. I've tried searching for an answer, but I've come up short. class A { friend void C::dec(A&); private: int field; }; class C { public: void dec(A&…
Donzulo
  • 19
  • 4
2
votes
2 answers

How to fix "invalid use of incomplete type" in c++

I wanted to make a friendship between 2 classes by using the method from one class. Even I looked into different tutorials and books and I can't make it works. Edit:: It works in one file, but I want to make it in separated files - what…
Sempron
  • 29
  • 1
  • 4
2
votes
2 answers

Why can not the operator<< for test2 + test3 be overloaded?

I try to overload the operator<<, but there is a warning that I cannot overload it. I can overload this operator as follows: std::cout << test4 << std::endl; But I can not overload it as follows: std::cout << test2 + test3 << std::endl; My main…
Youshikyou
  • 365
  • 1
  • 8
2
votes
1 answer

Friend Function = Operator Overloading of two different class

I am practicing operator overloading , i have done hundred times operator overloading but this time it's behavior is irritating if I compare this code with old code syntax (that complies fine) i find no change in syntax ,kindly guide me.Thanks EROR…
2
votes
1 answer

namespace in debug flags of in-class defined friend functions

I'm dealing with a class that defines a friend function in the class without outside declaration namespace our_namespace { template struct our_container { friend our_container set_union(our_container const &, our_container const &)…
pseyfert
  • 3,263
  • 3
  • 21
  • 47
2
votes
1 answer

C++ CRTP: How to make only one (some) function of the base class as a friend to to the derived class?

I would like to make ONLY Base::fct1() having access to class DerivedImpl members. Base looks like: template < typename Derived> class Base{ protected: void fct1(){ static_cast(this)->topfunc(); } void…
Courier
  • 920
  • 2
  • 11
  • 36
2
votes
2 answers

Befriending SFINAE checks

I've runned into some trouble trying to make friend declarations with sfinae checks (you can just jump into the code sample if you don't want explanations on "why" and "how"). Basically, I have some template class declaring two private member…
R. Absil
  • 173
  • 6
2
votes
2 answers

What is the meaning of angle bracket in the declaration of a function as a friend?

I have a hard time understanding the notation <> in the declaration of a function as a friend. (This is originated by needing to define the body of a function that was an injected friend into an external free function.) template class…
alfC
  • 14,261
  • 4
  • 67
  • 118
2
votes
1 answer

A Friend member Function can be used in separated files?

I am reading C++ Primer and I´m stuck at 7.3.4. Friendship Revisited Making A Member Function a Friend Exercise 7.32: Define your own versions of Screen and Window_mgr in which clear is a member of Window_mgr and a friend of Screen I have found…
Armando
  • 31
  • 4