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

Error while overloading increment operator inside overloaded I/O operator

I am a beginner to OOPS concept. I am working on Operator overloading now. I run into the error no match for operator<
0
votes
0 answers

If I have Friend I can add him again Laravel 5.1

I have a little problem. I created migration, model, etc about Friend, when I add Friend, everything is ok, but I do not know how to do that for a particular user in your friends, then I can not add it again. Then simply do not see the button. This…
Adrian
  • 93
  • 2
  • 11
0
votes
1 answer

Class member functions as friends in other classes C++

I have to classes that represent random generators. r_6.h #include #pragma once class R_G_6 { public: R_G_6() {}; float getNextRand(); void countFrequency(); void printFrequency(vector); …
niar_q
  • 153
  • 9
0
votes
1 answer

Friend functions and derived classes

I'm trying to derive a class from a Base that has a friend function defined. I want to create a friend function for my derived class that makes use of the Base's friend function, but preserves the type of my Derived class: #include class…
Joshua
  • 3
  • 3
0
votes
2 answers

c++ how to properly declare friend class method of another class

consider the following example. class A { int member; }; class B { A& a_ref; void manipulate() { a_ref.member++; } }; Now, obviously, B::manipulate can not access a_ref. I would like to allow (only) class B to get…
Zereges
  • 5,139
  • 1
  • 25
  • 49
0
votes
4 answers

Friend function is not accessing private members of another friend class

I have two classes Term and Polynomial. Polynomial class is declared to be a friend of Term class. Polynomial class has friend function in it. When i implement that function in a cpp file of Polynomial class, the private members of Term are not…
R32
  • 9
  • 1
  • 4
0
votes
2 answers

About the return type of an unbound template friend function of a template class

Suppose I have a template class which has a template friend function hoping to implement the function of a value multiplying an array(myArray): template class myArray{ T *_array; public: ... template
Shindou
  • 506
  • 6
  • 15
0
votes
1 answer

How to use 'make_ptr()' while T is a class with private construction?

I was working with singleton pattern and shared_ptr.I was trying to make the code like this: class A{ private: static std::shared_ptr instance; A(); public: static std::shared_ptr
user5065458
0
votes
1 answer

Why can't I access the private variable of a class after defining a friend function?

I've written a simple employee management project. I am facing problem when I am trying to assign values into the private variables of a class though I defined the operator overloading as friend function. Here is my…
0
votes
1 answer

Creating Friend Class instance in Function parameters

I am new to working with C++. I need to declare an instance of a class as the parameter of a function in another class, with the parameter instance declared as a friend. I illustrate with an example. class foo(){ private: void a(){ …
agm
  • 1
0
votes
1 answer

Why is including implementation for a friend function that uses template parameters inside of a template class without a template declaration compile?

Given the following code. #include template class Foo { public: Foo(const T& value = T()); friend Foo operator+ (const Foo& lhs, const Foo& rhs) { // ... } friend std::ostream& operator<<…
Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
0
votes
2 answers

How do I declare a template friend function that is type-specific in my template class?

I've recently learned that there are two ways to declare a template friend class or function. For example, to declare a template friend class, you may do this template class goo { template friend class foo; }; or…
Wandering Fool
  • 2,170
  • 3
  • 18
  • 48
0
votes
3 answers

Overloading friend operator method in template class

I'm trying to overload the assignment ('=') operator of a template class, but I need the operator= method to be a friend function. I though this would be simple, but I'm doing something wrong because the code below causes a compilation error: error…
VettelS
  • 1,204
  • 1
  • 9
  • 17
0
votes
2 answers

Can this be done without using friends?

Can the following code (I only kept the relevant part) be converted to use a static member function rather than a friend free function? If not, why not? I tried to convert it to use a static member function in multiple different ways and failed…
James Mazur
  • 3
  • 1
  • 2
0
votes
2 answers

How to use a common friend function to exchange the private values of two classes

I copy this program in my book.But i not understand one line in this program.This line is friend void exchange(class_1 &,class_2 &); My question is why use & operator in bracket? Please explain. #include using namespace std; class…
user2127324
  • 17
  • 1
  • 2
  • 7