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

Template friend declaration of a member of a dependent type

In the section "13.7.4 Friends" of the C++ 20 Standard there is an example of declaring a template friend declaration of a member of a dependent type. I have simplified the example leaving the declaration that arises a question. Here you…
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
4
votes
1 answer

Breaking of ODR inside one TU?

The following code compiles without any error, though it seems to break ODR: #include template class B; template struct A { template friend void ffoo(A a, B* = nullptr) { …
4
votes
1 answer

Should a friend function of a class template become friend to all instantiations?

Consider the following snippet: template struct Foo { friend void bar(Foo, Foo f) { static_cast(f.private_field); // Should only compile when friends with Foo. } private: int…
yeputons
  • 8,478
  • 34
  • 67
4
votes
3 answers

Why can't I call a template friend function with explicit template arguments?

Consider the following example: struct S { template friend void foo(S) { } }; int main() { S s; foo(s); // (1) foo(s); // (2) } My GCC 9.2.0 fails to compile (2) with the following error: a.cpp: In…
yeputons
  • 8,478
  • 34
  • 67
4
votes
1 answer

What is the right way to define a friend function outside a template class?

If I have a normal class I can "inject" a non-free friend function inside the class. (That among other things can be only be found by ADL). case 1: class A{ double p_; friend double f(A const& a){return a.p_;} }; If instead this is a template…
alfC
  • 14,261
  • 4
  • 67
  • 118
4
votes
3 answers

What is the namespace of a friend-defined function?

If I have a function defined as friend inside a class. What is the namespace of that function? namespace A{namespace B{ struct S{ friend void f(S const&){}; }; }} int main(){ A::B::S s{}; f(s); // ok ::f(s); // not ok,…
alfC
  • 14,261
  • 4
  • 67
  • 118
4
votes
2 answers

Are friend functions inherited? and why would a base class FRIEND function work on a derived class object?

class baseClass { public: friend int friendFuncReturn(baseClass &obj) { return obj.baseInt; } baseClass(int x) : baseInt(x) {} private: int baseInt; }; class derivedClass : public baseClass { public: derivedClass(int x, int y) :…
user8185371
4
votes
1 answer

Define friend function template of class template

I want to define a function template of a class template. The code looks like this. template struct test{ private: int value; template friend auto foo(test const t){ test r; r.value = t.value; …
4
votes
1 answer

Friend declaration in nested classes requiring forward declaration

I am trying to write non-intrusive boost::serialization routines for a nested class with a private member. Unfortunately, I fail to convince g++ that the serialization routine is a friend of the inner class. It seems g++ requires a forward…
4
votes
1 answer

how friend function has been implemented internally

We all have used friend function both global level as well as class level in C++. I tried to search accross the internet how internally friend function has been implemented. What manipulation done by the "friend" keyword. For exampale we know how…
Nihar
  • 347
  • 2
  • 16
4
votes
5 answers

How to split the definition of template friend function within template class?

The following example compiles fine but I can't figure out how to separate declaration and definition of operator<<() is this particular case. Every time I try to split the definition friend is causing trouble and gcc complains the operator<<()…
joke
  • 654
  • 9
  • 11
4
votes
1 answer

Friending template function from multiple classes

I have this code: template T f() { // ... } class A { friend A f(); }; class B { friend B f(); }; I get ambiguating new declaration of ‘B f()’ error. However, if I change my code to following template void f(T arg)…
Savenkov Alexey
  • 678
  • 4
  • 11
4
votes
6 answers

Using friend functions for I/O operator overloading in c++

I am learning c++ on my own. I was studying operator overloading, i was able to understand addition and subtraction operator overloading. But overloading of I/O operators is a bit confusing. I have created a class for Complex numbers, now i am…
user3834119
  • 411
  • 9
  • 21
4
votes
1 answer

Friend function undeclared identifiers

I'm working with OpenCV and Qt 5. I need to pass a mouse callback to a namedwindow for some work I'm doing. However, I can't get it to see any of the private member variables of my class. Here's some code: class testWizard : public QWizard { …
John
  • 347
  • 1
  • 7
  • 14
3
votes
2 answers

c++ error : (private data member) was not declared in this scope

Say I have a class like so: class Ingredient { public: friend istream& operator>>(istream& in, Ingredient& target); friend ostream& operator<<(ostream& out, Ingredient& data); private: Measure myMeas; MyString…
user637965