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
0
votes
1 answer

Friend Function Unable to access Private data member

I am trying to write friend function to addminutes in time and then change hour accordingly Eg: if Time t3(11,58,0) addMinutes(t3,10); t3.print() should give 12:08:00 PM below is the code which when I am trying to compile is giving 'minute' is a…
user8858671
0
votes
3 answers

Can not store any value

I have started learning C++ recently, doing some simple class/friend function practice, What i was trying to do is, get 2 numbers from user for 2 object of a class by using friend functions only then again using friend function only, multiply those…
Than
  • 3
  • 1
0
votes
0 answers

C++ friend function undefined?

I have a struct template A and B, an operator overload for A+int and a conversion from B to A. I want B to be converted to A when adding with int, so I used friend function declaration. #include template struct…
0
votes
1 answer

Interface with friend operator<<: why can't it link?

I have created a small interface for objects that can be represented in text mode using the operator<< like so: // ICliObject.h class ICliObject { public: ~ICliObject() = default; friend std::ostream& operator<<(std::ostream& p_stream,…
BobMorane
  • 3,870
  • 3
  • 20
  • 42
0
votes
1 answer

How to use pointer to friend function as a parameter of member function

class Btree{ friend void visitNode_(BtreeNode* node); void DFSshow(); void showNode_(BtreeNode* node,int step,void (*func)(BtreeNode*)); } template void Btree::DFSshow() { void (*ptr)(BtreeNode*); ptr =…
Ruoxuan.Wang
  • 179
  • 1
  • 2
  • 13
0
votes
1 answer

Why does this partial specialization of a template friend function work?

I am writing simple classes that implement vectors and matrices as part of trying to learn OpenGL. I have matrix and vector classes that look like this : // Forward declarations template struct vec; template
0
votes
2 answers

friend function not printing out what it should

whenever I run the program, there is no output, the program just ends. Am i doing something wrong? I'm sure there's something i missed but i can't seem to figure it out. #include #include using namespace std; class…
0
votes
1 answer

How do I access member variables from one class into other using friend functions in C++?

I am using fried functions for the very time and was assigned to complete an incomplete code using friend functions as below. //CODE GIVEN IN THE QUESTION NOT MEANT TO BE EDITED #include using namespace std; class store_keeper; class…
ba11b0y
  • 161
  • 3
  • 18
0
votes
1 answer

overloading pre-increment operator not showing correct result

I've overloaded the pre-increment operator using friend function. In the overloaded friend function, the value of the variable is showing correct. But that value is not shown in display function, why? #include using namespace std; class…
nischalinn
  • 1,133
  • 2
  • 12
  • 38
0
votes
1 answer

How to make all instances of a template function friends of a class?

I have a class which is meant to be constructed only by some factory functions and not directly. These factory function all have the same name, but are differentiated based on the enum value passed as their template argument (see the first method…
user6245072
  • 2,051
  • 21
  • 34
0
votes
1 answer

C++ error: passing xxx as 'this' argument of xxx discards qualifiers

This is the code I've written to access a matrix in a class and compare two objects of the same class using less than operator and equal to operator. But the compiler throws errors. #include using namespace std; class node…
ksharma377
  • 11
  • 3
0
votes
0 answers

friend a dependent typename of a template class

I have 3 classes, Device, Register, and WriteOnlyPolicy, defined as such: Device class Device { public: template inline void write(typename Register::value_type value) { Register::writeRegister(value, this); …
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
0
votes
1 answer

Linked list using class and friend functions: segmentation fault

I am unable to find the reason for the segmentation fault. I would really appreciate your help in any ways. Thanking you in advance. /A rudimentary linked list for implementing polynomials. This is just an experiment as I have no experience with…
0
votes
2 answers

C++ friend template function - minGW error but not VS2015

The following code compiles without a problem with Visual Studio 2015 but with minGW gets the warning and error shown below it: #include using std::ostream; template class Array { friend ostream…
BenevolentDeity
  • 693
  • 1
  • 4
  • 18
0
votes
1 answer

Friend functions not recognized

I have the following class with a couple friend functions: class Teleport { public: Teleport(); ~Teleport(); void display(); Location teleportFrom(int direction); friend bool overlap(Wall * wall, Teleport * teleport); friend…
wrongusername
  • 18,564
  • 40
  • 130
  • 214