In object-oriented programming to allow access to "private" or "protected" data of a class in another class, the latter class is declared as a friend class.
Questions tagged [friend-class]
83 questions
46
votes
3 answers
'friend' functions and << operator overloading: What is the proper way to overload an operator for a class?
In a project I'm working on, I have a Score class, defined below in score.h. I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is printed.
Here's what I tried to do:
ostream & Score::operator<< (ostream &…

F. P.
- 5,018
- 10
- 55
- 80
30
votes
2 answers
Declaring a namespace as a friend of a class
I was wondering if there is a way such that we make all functions defined within a specific namespace friend with a class?
In particular, I have a class, for example:
class C {
private:
// ...
public:
// ...
friend…

MikeL
- 2,369
- 2
- 24
- 38
26
votes
2 answers
Friend class in TypeScript
In C++ there is something called a friend class. As far as I know, there's no such thing in TypeScript/JavaScript. Is there a way to simulate such behavior of friend class in TypeScript/JavaScript?
To give better context (if needed) of why and what…

kamyl
- 5,938
- 4
- 23
- 29
23
votes
7 answers
What is the equivalent of a 'friend' keyword in C Sharp?
What is the equivalent of a 'friend' keyword in C Sharp?
How do I use the 'internal' keyword?
I have read that 'internal' keyword is a replacement for 'friend' in C#.
I am using a DLL in my C# project that I have the source code for and yet I do not…

xarzu
- 8,657
- 40
- 108
- 160
17
votes
8 answers
Why friend directive is missing in Java?
I was wondering why Java has been designed without the frienddirective that is available in C++ to allow finer control over which methods and instance variables are available from outside the package in which a class has been defined.
I don't see…

Jack
- 131,802
- 30
- 241
- 343
16
votes
4 answers
Why can I not instantiate a class whose constructor is private in a friend class?
I have two classes; Salary that is intended to hold information and calculations regarding the salary of an employee and Employee that has an object of type class Salary and some members like name and address of the employee...
What I want to do is…

Syfu_H
- 605
- 4
- 17
14
votes
2 answers
are copy and move constructors automatic friends?
We can access private variables of another class when we define copy or move constructors. Does C++ make them friend to each other automatically?
For example:
my_str::my_str(my_str&& m)
{
size_ = m.size_; //accessing private variable another…

redkont
- 337
- 1
- 2
- 12
12
votes
4 answers
How come forward declaration is not needed for friend class concept?
I've just recently learned about friend class concept in C++ (I've googled around for a bit, but this answer made me laugh until I remembered the most important parts), and I'm trying to incorporate it in to the project I'm working on right now.…

penelope
- 8,251
- 8
- 45
- 87
12
votes
6 answers
C# Friend classes and OOP Composition
Given class A, which contains sets of raw data, and class B, which contains a re-organized version (GUI ready) of that data I would like to make the raw data in A visible in B.
Clearly the raw data in class A is contained in private members. I would…

Rire1979
- 652
- 12
- 24
11
votes
6 answers
Using a friend class vs. adding accessors for unit testing in C++?
Is it better to add functions that return the internal state of an object for unit testing, as opposed to making the testing class a friend? - especially, when there is no use for the functions except for the case of unit testing.

bias
- 1,467
- 3
- 19
- 39
9
votes
1 answer
A friend function accessing a private member of a friend class
Following the Czech song from Eurovision 2019 in Tel-Aviv
It is known that in C++ a friend of a friend is not (automatically) a friend.
Clang however differs on the following code with GCC and MSVC:
class A {
public:
// forward declaration
…

Amir Kirsh
- 12,564
- 41
- 74
9
votes
2 answers
C++ Templates: Partial Template Specifications and Friend Classes
is it possible to somehow make a partial template specification a friend class? I.e. consider you have the following template class
template class X{
T t;
};
Now you have partial specializations, for example, for pointers
template…

gexicide
- 38,535
- 21
- 92
- 152
8
votes
4 answers
recursive friend classes
Is there any way around this:
class B;
class C {
public:
C() { }
private:
int i;
friend B::B();
};
class B {
public:
B() { }
private:
int i;
friend C::C();
};
Gives error:
prog.cpp:8: error: invalid use of incomplete type…

BCS
- 75,627
- 68
- 187
- 294
8
votes
3 answers
Inheriting friendship in C++?
Since class friendship is not inherited in C++, what's the best way to "fake" it?
I was thinking about exposing the friend class's private interface through protected methods in the to-be-inherited base-class, but that results in having to write…

sold
- 2,041
- 5
- 25
- 32
7
votes
2 answers
"Friend Classes" in javascript
I have a Factory class that creates a Widget object. The Factory object needs to callback a "private method" of the Widget object at a later time to pass it some ajax info. So far, the only implementation I've come up with is to create a public…

Derg
- 281
- 3
- 8