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

error C2248 cannot access private member declared in class

We got an exercise in c++. The teacher gave us the functions in the public part of the "class Assignment"(so I cannot change the public declaration of the functions in the header.h). I got an compilation error when i tried to make a friend cout…
-2
votes
1 answer

can we define friend function inside the class in cpp

according to rule we cant define friend function inside the class for istream and ostream we always declare friend function (so it is right) in below code But the problem for 1st point how it can be possible for given code to run successfully…
-3
votes
1 answer

c++ how does friend function differs in memory location?

I am trying to understand how defining a function as a friend function impact its placement in memory (RAM). For example, each class has a table of all of its methods and functions. Also, virtual functions are placed in the vtable. Where does friend…
ThunderWiring
  • 738
  • 1
  • 7
  • 29
-3
votes
1 answer

trouble with friend function

so my program is that i want the user to enter information about a board so everything is working except the part where i need the user to enter the value of attitude then add another number to increase that attitude so I'm stuck at this. As you can…
Caroline
  • 1
  • 1
  • 4
-3
votes
1 answer

Why is the error "not declared in this scope" shows?

I am a beginner in C++. I am learning the topic friend functions. I have the code below in which two friend functions are declared in the class and called by the constructor but an error shows that the declared friend member functions are not…
Kiran C K
  • 67
  • 7
-3
votes
2 answers

declaration of âoperator<<â as non-function

i have tried everything , i can't understand why it's not working. g++ throw : Queue.H:53: error: declaration of âoperator<<â as non-function Queue.H:53: error: expected â;â before â<â token Queue.H:59: error: expected â;â before âprivateâ the…
oren
  • 9
-4
votes
2 answers

Friend function cannot access private member

I want to access the private objects of the class MainWindow with the friend function void recognized(RecoResult *result) that has already another namespace LapsAPI::RecoResult in the parameter result. How can i access Ui::MainWindow *ui inside of…
-6
votes
1 answer

using Friend function and operator+ to add class variables and print out

I've written a code which takes an input of 3 integers (integer, numerator and denominator) and displays them as "a{b/c}" I have defined/declared a class (fraction) which contains these 3 separate integer variables and the functions to read and…
1 2 3
24
25