Questions tagged [friend-class]

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.

83 questions
3
votes
3 answers

Understanding member access with inheritance / friend class in C++

from C++ primer 5th edition: have a look at these classes: class Base { friend class Pal; public: void pub_mem(); protected: int prot_mem; private: int priv_mem; }; class Sneaky : public Base { private: int j; }; class Pal…
Jason
  • 147
  • 1
  • 1
  • 7
3
votes
2 answers

Defining an stl container for a class that has just been declared.

I am trying to implement a finite element code for my research. I need to create a map that associates materials with their names so that I can access them by name. From the header file of the main class, I define the following: /// Friend class…
2
votes
1 answer

C++ detail namespace vs anonymous vs private method to class vs. pimpl vs. friend class

Bear with me here. I am trying to figure out at what point do you draw the line regarding putting helper methods in anon vs. detail namespace vs. private vs. creating pimpl or friend class. Here's my take.. Please let me know your thoughts. So,…
thassan
  • 391
  • 3
  • 15
2
votes
1 answer

C++: how to make a group of derived classes be able to access private members of one class?

Suppose a class: Library And we have a group of derived class from the base class LibraryCustomer, such as: Kid, Parent, Student, etc In the class of Library, there are a group of (tons of) private members variables. Since there are tons of private…
user1914692
  • 3,033
  • 5
  • 36
  • 61
2
votes
1 answer

Friends and nested classes

Ok I'm totally frazzled on this. Code is begin to swim around the screen...must sleep. So! Ok, troubled by nested classes and friends. here is the pseudo-code class A{ public: //constructor // member functions …
2
votes
1 answer

friend class with forward class declaration does not compile

This a basic program to understand how to use friend class in C++. Class xxx has a class yyy object using friend. Since class yyy is defined after class xxx I have declared class yyy using forward declaration. #include using…
Santosh Sahu
  • 2,134
  • 6
  • 27
  • 51
2
votes
2 answers

Access Mysql * connection variable from different c++ class

I wrote c++ class to connect to mysql database: hpp file #include #include #include "mysql/mysql.h" #ifndef _DATA #define _DATA class Database { public: string host; string user; string pwd; string db; …
Begayim Muratalina
  • 621
  • 1
  • 6
  • 12
2
votes
2 answers

Using friend function in C++

Just read about friend functions and I'm trying to access private variable "number" in class A with friend function "Print" from class B. I'm working with Visual Studio. Compilation of my code gives me plenty of various errors like: C2011: 'A' :…
QED
  • 77
  • 1
  • 8
2
votes
6 answers

Why we need a "friend" here? (C++)

The qml viewer (for 4.8 and 5.0) is implemented like that: In the .h(eader) we have: class QtQuick2ApplicationViewer : public QQuickView { Q_OBJECT ... private: class QtQuick2ApplicationViewerPrivate *d; }; Then in the .CPP file: class…
Zingam
  • 4,498
  • 6
  • 28
  • 48
1
vote
2 answers

friend class doesn't do well with me?

I am trying to deal with friend class for the first time. I wrote the code below: class Kind{ private: friend class Type; int x; public: Kind(){ x=0; } void setX(int X) { x =X; } int getX() { return x; } }; class Type:…
Aan
  • 12,247
  • 36
  • 89
  • 150
1
vote
1 answer

How to declare a template friend class inside other template class?

Hello. I'm trying to implement double linked list. I have a linked_list class and DListNode class as a shell for elements in my container and I prefer DListNode as a class rather than as a struct, because I want node fields to be inaccessible…
getsuga
  • 15
  • 5
1
vote
2 answers

Can't access private constructor from a friend class

In the following code snippet, g++ compiler outputs the following error: error: ‘B::B(const string&)’ is private within this context 857 | { return unique_ptr<_Tp>(new _Tp(std::forward<_Args>(__args)...)); } Commenting out the line where smart…
1
vote
5 answers

Why does friend class crash on static function call?

#include using namespace std; class CClass { private: friend class CFriend; static void privateFunc(){std::cout << "privateFunc" << std::endl;}; }; class CFriend { public: void privateFunc(){privateFunc();}; }; int main(int…
AudioDroid
  • 2,292
  • 2
  • 20
  • 31
1
vote
0 answers

Friendship between two private, nested classes

I have 4 classes, A, A1, B, and B1. class A contains a private nested class A1, and class B contains a private nested class B1. I want the constructor for class B::B1 to have access to the private data members of objects of type A::A1. My solution…
mana
  • 545
  • 4
  • 12
1
vote
1 answer

Unable to use brace enclosed initializer-list while inheriting from friend class

I am trying to use an initializer list for a data structure which inherits from its parents friend class's subclass. Below I compiled an example which demonstrates the problem(in c++11). #include #include class i_gossip; class…
Dávid Tóth
  • 2,788
  • 1
  • 21
  • 46