Questions tagged [friend]

In object-oriented programming, friend refers to a method or class that has access to some non-public aspects of a particular class.

In object-oriented programming, friend refers to a method or class that has access to some non-public aspects of a particular class.

Different programming languages use the friend keyword differently. Make sure it is clear which language you are asking about.

1518 questions
0
votes
2 answers

How to pass objects as arguments?

I have changed the code here all the variables have to remain private and using friend functions is a requirement. Here there are two classes A and B and I am supposed to accept 5 numbers(void enter()).The function average is to be able to access…
G-K
  • 13
  • 3
0
votes
1 answer

make an inner class a friend in C++

I want to make an inner class a friend of an unrelated class but this doesn't seem to work (at least in gcc 4.1.2): class A { int i; friend class B; // fine friend class B::C; // not allowed? }; class B { int geti(A* ap) { return…
dave
  • 21
  • 1
  • 2
0
votes
1 answer

How to overload == operator without making it a friend function?

I have a class SomeClass and I wish to implement an overloaded == to compare two instances of this class. The overloaded == does not make use of any of SomeClass's private members. So, it does not have to be a friend. How do I make it a non-member,…
Train Heartnet
  • 785
  • 1
  • 12
  • 24
0
votes
1 answer

friend of a class doesn't access private values of a class in CPP

I want to ask a question about friends of a class in C++. I am a beginner in C++ and learning about overloading operators as global functions. I wrote the following part of a class declaration in the Mystrings.h file and the corresponding function…
bodn19888
  • 167
  • 10
0
votes
2 answers

Friend function used for output stream operator shown error as not accessible

I have written simple class like below in file VRKVector.h namespace vrk { class VRKVector { private: std::vector m_coordinates; int m_dimension; public: /** VRKVector constructor. * @param coordinates which…
venkysmarty
  • 11,099
  • 25
  • 101
  • 184
0
votes
1 answer

How can I use static members in friend functions without prefixing [class name]::

I have this code in Player.h class Player { friend void clearBoard(); private: static char board[4][4][4]; }; and this code in Player.cpp char Player::board[4][4][4] = {}; void clearBoard() { for (byte i = 0; i < 4; i++) for (byte j =…
beardeadclown
  • 327
  • 2
  • 14
0
votes
0 answers

How derivation access specifier and friendship interact?

I have a bit confusion about inheritance and friendship. Consider this example: class D; class A { protected: int x = 0; private: int y = 1; public: int z = 2; friend class D; }; class…
Maestro
  • 2,512
  • 9
  • 24
0
votes
0 answers

Friend function wont work due to undeclared identifier

This is the Foo.hpp #ifndef Foo_hpp #define Foo_hpp #include #include "Bar.hpp" class Bar; class Foo { private: int z; public: void addY(Bar &x); }; #endif /* Foo_hpp */ This is the Bar.hpp #ifndef Bar_hpp #define…
prayoga yoga
  • 133
  • 7
0
votes
1 answer

Friend function in class defined in namespace

I'm trying to make a function in DataBase namespace declared in DataBase.h file with implementation in DataBase.cpp that needs an access to a protected member of Collection class. Here is what i currently have Collection.h: class Collection { …
0
votes
1 answer

Why does my compiler insist that operator<< has 3 parameters when it has 2?

This seems so simple, and I've overloaded operators before, but now i get the error message error: overloaded 'operator<<' must be a binary operator (has 3 parameters). I feel like there is something obvious I'm missing, but after googling for some…
0
votes
2 answers

Reciprocal friend member functions = circular include declarations

I have two classes defined in different h files and each class has some private functions. However, there's one function in each class that I want to be able to access from a function in the other class. For example... //apple.h: class…
Matt
  • 1,327
  • 1
  • 12
  • 21
0
votes
2 answers

Friend, Forward Declaration, C++

So this is confusing me a little. Neither will be declared before the other, incomplete types or such. Game won't go before Hero because of Hero test1. Hero won't go before Game because of friend void Game::vitals(Hero&); Is this an impossible…
Sean
  • 31
  • 4
0
votes
0 answers

How do I make a friend function for this code?

I want to make a friend function to display the Staff member that gets paid the most out of three staff members. I am unsure how to go about this. My code already works, just wanna add to it. What would the code be to do this and why does it work??…
0
votes
2 answers

How to set Variadic CRTP base classes to be friend's of the derived class

Main parts of the problem is using CRTP with a policy based design and variadic template. From the policy can't reach the protected or private members from the main/derived class. Because of using variadic template I can't declare policies just as…
0
votes
0 answers

dependent classes friend functions, are they possible?

I was reading an old C++ book to do some retro coding, C++ for C Programmers by Sharam Hekmatpour, I remember reading it when I was way younger and I liked it. In the friend functions chapter he shows having two classes with friend functions: class…
cprieto
  • 400
  • 2
  • 14