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
7
votes
1 answer
What is the difference of friend iterator and friend class iterator which encounter in thinking in c++?
In Thinking in C++ Volume 1, chapter 16: Introduction to Templates.
The context:
Notice that instead of just saying:
friend iterator; // Make it a friend
This code has:
friend class iterator; // Make it a friend
This is important because…

Alister
- 71
- 1
6
votes
2 answers
Can an injected class name be used as a type name in a friend declaration?
Consider this code:
template
class Singleton
{
};
class Logger : public Singleton {
friend class Singleton;
};
It compiles in gcc and clang, but is it valid? [temp.local].1 says:
When it is used with a…

Nikolai
- 3,053
- 3
- 24
- 33
6
votes
1 answer
Template Friend Class: Forward Declaration or...?
Suppose I have a template class that I am trying to declare as a friend class. Should I forward declare the class or give it its own template?
Example:
template
class SLinkedList;
template
class SNode {
private:
E elem;
…

badfilms
- 4,317
- 1
- 18
- 31
5
votes
4 answers
Definition of friend class and accessor sections
When defining a class as a friend class, does it matter in which accessor section the definitions is placed, and if so does that change the members the friend has access to?
class aclass
{
private:
// friend bclass;
public:
// friend…

Zamfir
- 129
- 1
- 4
5
votes
4 answers
Friend Class In C++
here im not understanding the concept very well or i am right.... So lets take this "friend" class example here:
class MyClass{
friend class AnotherClass;
private:
int secret;
}
class AnotherClass{
public:
void getSecret(MyClass mc){
…

amanuel2
- 4,508
- 4
- 36
- 67
5
votes
1 answer
Two classes with friend methods in C++
Currently I am reading a book about C++ and it has some exercises. One of the exercises asks to build two classes where each has a friend method for another. My current guess looks like this:
#include
using std::cout;
using…

gkuzmin
- 2,414
- 17
- 24
4
votes
2 answers
How to allow a std::unique_ptr to access a class's private destructor or implement a C++ factory class with a private destructor?
I'm quite far into the development of a game using SDL, OpenGL and C++ and am looking for ways to optimize the way the game switches between GLSL shaders for lots of different objects of different types. This is much more of a C++ question than an…

Jaymaican
- 149
- 6
4
votes
1 answer
C++: Can a Template be friend of a Class?
Is it possible to make friend of a class, all possible variants of a class-template?
Just to clarify, for example, something like this:
class A
{ friend template B; } // syntactic error yeah
So any B variant could manipulate any protected…

Ali Rojas
- 549
- 1
- 3
- 12
4
votes
2 answers
friend class and the scope of function arguments
I'm writing a reflection utility for self usage, I simplified the code(remove complicated templates) as below:
class A {
private:
friend class Field;
int i;
};
class Field {
public:
Field(int A::* p) : p(p) {}
private:
int A::*…
user2269707
4
votes
1 answer
Can a friend class create objects from its friend classes in C++?
These are the code inside my two C++ header files where I declare two classes, one is friend of the other:
==>The first class creates a hash table and fills it with words from a given file.
#include "remove_duplicates.h"
class…

magmine
- 164
- 2
- 15
4
votes
1 answer
C++ Accessing a private member in a friend class
I have 2 classes (firstClass and secondClass) which firstClass is a friend of secondClass, and has a private nested unordered_map, which I want to access it in a function of secondClass.
So basically the code is like this:
class secondClass;
…

Hakim
- 11,110
- 14
- 34
- 37
3
votes
2 answers
Two Classes Befriending Each Other
I'm trying to make two classes friend with each other, but I keep getting a "Use of Undefined type A" error message.
Here is my code:
I've tried to add class A; as shown on top but still the same.
#include
class A;
class B
{
private:
…

محمد خير الخلق
- 153
- 1
- 9
3
votes
0 answers
Friend class read only access C++
My question is somewhat similar to the title of friend class with limited access. But the limited access I define by 'access-only'. I mean, I've a main class which does the work and creating a helper class which is useful for displaying the…

vpshastry
- 81
- 1
- 7
3
votes
5 answers
Keeping part of public nested class visible only to the nesting class
I have a nested class in c++ which has to be public. But I need some of its methods visible to the outer world, and the rest visible only to the nesting class. That is:
class set {
public:
class iterator {
innerMethod();
public:
…

Mockingbird
- 31
- 1
3
votes
1 answer
C++: grant access to private operator "passing through" a friend class, possible?
Let's say I have defined these two classes:
class Node
{
friend class list;
public:
Node (const int = 0);
Node (const Node &);
private:
int val;
Node * next;
Node & operator= (const Node &);
};
class list
{
public:
…

grd
- 287
- 1
- 2
- 8