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
1
vote
1 answer
Cannot access private member declared in class, even declared friend class
I have two classes:
class CALLDB;
class CALL
{
friend class CALLDB;
public:
string GetStart()const;
private:
string start;
};
And second class:
class CALLDB
{
friend class CALL;
public:
unsigned int Load(istream& fin);
private:
unsigned int…

Tianyi Liu
- 31
- 1
- 4
1
vote
3 answers
how to link two template classes in many-to-many friendship?
assume that I have the following two template classes :
template
class First
{
private:
int a;
};
template
class Second
{
private:
int b;
};
how can I link them in many-to-many friendship. for example, adding a…

Tamer Shlash
- 9,314
- 5
- 44
- 82
1
vote
2 answers
C++ friend class of the same name in different levels of a nested namespace
Not sure if this is possible, but I have two classes of the same name in different levels of a nested namespace and I'd like to make the more shallow class a friend of the deeper class. Example:
In File1.h:
namespace A
{
class Foo
{
//stuff
…

T. Cushman
- 31
- 3
1
vote
3 answers
friend function and include loop
I've got a problem with friend function between two classes. Lets see some code:
First class:
#ifndef _FIRST_H_
#define _FIRST_H_
//#include "Second.h"
#include
class Second;
class First
{
friend void Second::fun();
std::string…

QueUe
- 91
- 4
- 7
1
vote
3 answers
Is there a way to make all derived classes friends of one another?
And if so, in what situation might this be useful?
Or (and I imagine this is the case), why is it absolutely useless? (What other approach essentially covers the abilities afforded by such friendship, but in a safer and less-leaky way?)
I was in a…

Andrew Cheong
- 29,362
- 15
- 90
- 145
1
vote
2 answers
How to access private members of an abstract class from its friend class?
class A
{
private:
int a,b,c;
public:
virtual int get()=0;
friend class B;
};
class B{
//here I want to access private variables of class A that is a, b and c
};
class C:public class A
{
int get(){
…

Karup
- 2,024
- 3
- 22
- 48
1
vote
2 answers
How to declare a specialization of a template's friend function
Having a template:
template > class Cont=std::vector>
class VehiclesContainer {
public:
VehiclesContainer(std::initializer_list l):container(l){};
virtual…

Avraam Mavridis
- 8,698
- 19
- 79
- 133
1
vote
2 answers
How to access methods and fields of the nested class from enclosing template class?
I've searched through the web an didn't find any explanation why the following happens.
For example, there is a template class Enclosing with a nested class Nested.
In Enclosing class, there is a method that should create an instance of the Nested…

Grinch
- 13
- 3
1
vote
1 answer
Can a friend class invoke a private constructor in c++? (and what's Singleton)
Consider class A and B. Class A is befriended by class B. Class B has a private constructor. Can class A create class B instances, or is a private constructor an indication to the compiler that the class cannot be instantiated, even by a friend…

Codesmith
- 5,779
- 5
- 38
- 50
1
vote
2 answers
Overloading operators as friend
I have an idea of the use of the word friend, to access to a private members besides own class. For instance, I have a class A and need to access to a private method of an attribute which is of class B inside a method of A I could declare the method…

dmayola
- 502
- 5
- 16
0
votes
0 answers
Exposing only a single method of a class to another class but having it inaccessible everywhere else
I wonder if there is some non-ugly way to expose only one method of a class to only one class.
I come up with this solution and wonder if there is any other better (shorter) way to achieve the same?
class BarMethodGuard {
private:
friend class…

Edvard Davtyan
- 73
- 5
0
votes
0 answers
Accessing a class from another class in my Student Management Systems in C++
I am trying to create a student management systems using classes and I am a beginner in OOP because my school does not include OOP in my IT course.
I am trying to view All Subjects in the TEACHER MAIN MENU and the problem is the result is always…

MN Narvaez
- 27
- 3
0
votes
1 answer
Friend classes in the same namespace
I was programming Vulkan based renderer but unfortunately this error appeared:
'Azazel::Instance::~Instance': cannot access private member declared in class 'Azazel::Instance'
It gives me a headache. I tried to predeclare Azazel_Menager class at…

Jakub Chomicz
- 1
- 1
0
votes
1 answer
Can't change the value of private member of a class using friend class
So I was trying to learn how to change the value of the private class member using friend class, but the friend class cannot change the value of the main class, here is the code I have done, I am new in the world of coding, please help me out…

TOUSHIF HOSSAIN
- 13
- 5
0
votes
2 answers
Why doesn't the compiler like all the strings that are associated with "Iter"?
I am implementing one of the behavioral design patterns.
The compiler gives errors in lines that are related to "Iter". I don't understand at all why this happens, and even more so, how can it be fixed?
I thought that maybe there was a clerical…

dbUser11
- 305
- 1
- 10