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

Member access control for friend function defined inside class in C++

I understand that the following C++ code snippet should produce an error in the definition of g, because p.t.x is private and cannot be accessed there. class P { class T { int x; friend class P; }; T t; friend void g(P &p); }; void…
nickie
  • 5,608
  • 2
  • 23
  • 37
6
votes
0 answers

C++ Friend function of nested class in class template

I'm having the following problem. The code below runs fine on gdb online, however locally compiling like this: /.../g++ -std=c++17 -g -O3 /.../Test.cpp -o /.../Test produces: error: 'privateMember' is a private member of…
Michał P.
  • 113
  • 6
6
votes
2 answers

Restrict the scope of class instances accessible by multiple template parameter friend function

I would like to know if what I am aiming for is possible. I have a class Class such that #include template class Class; template Class f(Class& C, const Class& D); template class Class…
user9063318
6
votes
4 answers

c++ implicit conversion on user-defined operator for template classes

I have a struct template A and a + operator with int. #include template struct A{ int a; }; template int operator+(A a, int b){ return a.a+b; } I created a struct template B, which is convertible to…
eivour
  • 1,678
  • 12
  • 20
6
votes
1 answer

ADL and friend injection

Consider this code: template struct X { friend void f(X *) {} }; int main() { f((X<0> *)0); // Error? } compilers seem to heavily disagree. (MSVC08/10 says no, GCC<4.5 says yes, but 4.5 says no, sun 5.1 says yes, intel 11.1 says yes too…
uj2
  • 2,255
  • 2
  • 21
  • 32
6
votes
4 answers

template friend functions of template class

I have the following template class and template function which intends to access the class' private data member: #include template class MyVar { int x; }; template void printVar(const MyVar& var) { …
timrau
  • 22,578
  • 4
  • 51
  • 64
6
votes
3 answers

Overload operator<< for template class

I am having problem with overloading operator<< for a template class. I am using Visual Studio 2010, and here is my code. #ifndef _FINITEFIELD #define _FINITEFIELD #include namespace Polyff{ template class…
5
votes
3 answers

C++0x, user-defined literals with friend operator ""()

Will it be possible and/or useful to define an operator "" (...) as a friend function? class Puzzle { friend Puzzle operator "" _puzzle(const char*, size_t); ... }; void solve(Puzzle); int main() { solve("oxo,xox"_puzzle); }; I am thinking…
towi
  • 21,587
  • 28
  • 106
  • 187
5
votes
1 answer

Why are friend functions "available" for derived classes only when using public inheritance?

When a derived class inherits from a base class via public access, the question is the same as that in Are friend functions inherited? and why would a base class FRIEND function work on a derived class object? . However, if it inherits via protected…
JM233333
  • 53
  • 5
5
votes
1 answer

Using friend function, can we overwrite the private member of the class?

In the given c++ code the private member of class DEF is getting initialized in the constructor and again inside the friend function. So the redefinition will overwrite the private variables or the value given by the constructor will…
5
votes
2 answers

Error on MSVC when trying to declare std::make_unique as friend of my templated class

Apparently today, MSVC is trying its best to convince me to switch to clang. But I won't give up. Earlier I asked this question wondering how to declare std::make_unique as a friend of my class. I got a pretty good answer on my simple scenario and…
TheCrafter
  • 1,909
  • 2
  • 23
  • 44
5
votes
1 answer

c++ inline friend function with same name as member variable

This surprised me. This works: struct foo { int x; friend int x(foo f) { return f.x; } friend int y(foo f); }; int y(foo f) { return x(f); } // no problem But this is an error: struct foo { int x; friend int x(foo f) { return f.x; } …
dmitchell
  • 51
  • 2
5
votes
3 answers

cannot convert '*void(MyClass::*)(void*) to void*(*)(void*) in pthread_create function

i'm trying to create a new thread with a class "CameraManager" but i have the following error: cannot convert '*void(CameraManager:: * )(void*) to void*( * )(void*) in pthread_create function i defined in the cameramanager.h file: public: void…
4
votes
4 answers

overloading operator<< to output object members without using friend function

I am refreshing cpp after a long gap, trying to understand the operator overloading methods. I tried to overload "operator<<" to output members of object. but I am unable to do so without using friend function. I am looking for a method without…
chinnagaja
  • 155
  • 2
  • 4
  • 14
4
votes
2 answers

How to resolve "class must be used when declaring a friend" error?

class two; class one { int a; public: one() { a = 8; } friend two; }; class two { public: two() { } two(one i) { cout << i.a; } }; int main() { one…
shubhendu mahajan
  • 816
  • 1
  • 7
  • 16
1 2
3
24 25