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

How to provide protection to friend function in C++

I have just started learning friend functions in C++.This is the program which I am using for the concept exploration. #include using namespace std; class one { private: int age; public: one() { age=1; } void…
Naveen
  • 7,944
  • 12
  • 78
  • 165
0
votes
1 answer

operator overloading >> and private member

Please do read before flagging as a duplicate I am overloading operators >> and << for reading complex numbers with real part r and imaginary part i; #include #include #include class complex { int…
0
votes
2 answers

C++ friend functions

My question is pretty simple. I'm learning about friend functions, but this does not work for some reason. It only words if i swap the screen class with the Window_Mgr class, and then add a forward declaration of the screen class. Does it not work…
Oliver
  • 111
  • 2
  • 10
0
votes
1 answer

Instantiate an Object of a Class in completely specialized template function

I don't know why i am not able to instantiate an Object of the class B in a completely specialized function in spite of declaring the function as a friend in the class B.Please help.I don't know if its a silly doubt but I am Learning C++ Templates…
Anderson neo
  • 195
  • 2
  • 7
  • 17
0
votes
3 answers

Is there a way to overload extraction operator, inside a class and not as a friend of class?

I am trying to overload << operator, so that when I use my class object in std::cout, it prints out the data members which is supposed to get printed. I know I have to define a friend functions signature inside of a class, and then outside of the…
Hossein
  • 24,202
  • 35
  • 119
  • 224
-1
votes
1 answer

friend function is not a friend of all template instances?

In the following example I try to access the private member function subscribe() from a templated class type from within its friend function. However it appears that the friend function is only befriended in one instantiation of the class and not…
glades
  • 3,778
  • 1
  • 12
  • 34
-1
votes
2 answers

Why isn't it possible to overload the subscript operator (operator[]) as a friend function?

Why we cannot overload subscript operator using friend function
-1
votes
2 answers

declare a function in a .h (library) file as a friend to a class

I’m writing a simple class for list of products and there I need to overload both extraction >> operator and insertion << operator to write into file and read from file student_projectV1/list.h #ifndef STUDENT_PROJECTV1_LIST #define…
solo
  • 25
  • 6
-1
votes
1 answer

friend function in C++ is not accessing private members

I am learning C++ and I have written the below given simple program to understand the working of friend function (ignore all the complication I made by using complex syntax in the code because I am learning and I practice the syntax that I learn in…
Ahsan
  • 11
-1
votes
1 answer

friend function cant access private memebers

i started learning about operators overloading, at first it seem to easy, but now am having a problem accessing private member when try to make a global funtion operator player.hpp #ifndef _PLAYER_HPP_ #define _PLAYER_HPP_ #include…
-1
votes
1 answer

A problem after changing friend function to member function in C++ operator overloading

To overload operator +, I first implement that as friend function + normal function class Cents { private: int m_cents; public: Cents(int); int getCents() const; friend Cents operator+(const Cents&, const Cents&); }; Cents::Cents(int…
Simon Z.
  • 598
  • 5
  • 11
-1
votes
1 answer

why is cout calling parent operator<< instead of child operator<<

I have 3 classes, and I'd like each one to print out differently to the terminal, I have a node class that represents a vertex in a BDD graph, right now I'm trying to write code to do logical operations on the nodes. The Node class is setup as…
iggy12345
  • 1,233
  • 12
  • 31
-1
votes
1 answer

Friend function of C++ in class

**If i have friend function in class A and there is no member function in Class A.Then can i access class A useing that Friend function ** if not than how can access that class?
-1
votes
1 answer

How can I use a nested class from a friend function?

Just as I get one solution, I have another problem. See, I have a friend statement in my templated linked list, and I need to have it as a friend to reach my private nested Node struct. template class LL; template
user11360291
-1
votes
1 answer

doing operator overloading and polymorphism correctly

I have two wrapper classes for string and int and one to represent Binary Operation and overloading the operator << to write them in a string format. Unfortunately, I can't overload << without friend function and I can't get polymorphism without…