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

Override virtual protected method that is a friend of another class

Basically, I want to somehow simulate friendship inheritance with the restriction that it can only happen from inside a certain method. So essentially, this is what I want class A; // Forward declaration class Base{ friend class A; // friend…
GamefanA
  • 1,555
  • 2
  • 16
  • 23
0
votes
2 answers

Operator Overloading using friend functions in C++

I have the following Point class. #ifndef POINT_HPP #define POINT_HPP #include class Point { private: double m_x, m_y; public: Point(); Point(double x, double y); Point(const Point &p); ~Point(); //…
Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66
0
votes
1 answer

Friend template declaration with nested class as argument

The C++ FAQ offers guide how to write friend template declarations. I have a problem though when one of the arguments is a nested struct of the template class, e.g.: template class MyClass; template QDataStream…
Resurrection
  • 3,916
  • 2
  • 34
  • 56
0
votes
1 answer

C++ how to use key word 'friend' with member functions from two classes contain with each other

I'm new learning c++ How do I use friend with member functions from two classes contain with each other? I could not find a good answer through Google Below is my code: #ifndef FriendTest_hpp #define FriendTest_hpp class FriendVisitor; class…
JerryLin
  • 119
  • 1
  • 5
0
votes
2 answers

Using stream operator with Template Class

I am creating a matrix class and have the following declaration. The intent is to build a scalable matrix class that has algorithms that are flexible and can be run on various kinds of platforms - template class xss_matrix{ public: …
Rajat Mitra
  • 167
  • 1
  • 11
0
votes
1 answer

using friend function with const reference in operator overloading

The code below cannot be compiled. However, when I remove "const" from Point& of the friend function, this code turns to be compiled. Could anyone explain the reason why? class Point { public: Point(double x, double y); Point…
orematasaburo
  • 1,207
  • 10
  • 20
0
votes
1 answer

Difference between nested class and befriending a derived class in base class

What is the difference between creating class P with all members as private and befriending class B which is derived from P class P { private: int i; friend class B; }; class B: public P { public: void get() { …
anurag86
  • 1,635
  • 1
  • 16
  • 31
0
votes
0 answers

friend declaration of interface class in abstract base class

So I was thinking about trying to program the tetris game in c++ with the sfml and I thought about how I could implement the game and realized it could be a could exercice on inheritance. I thought I will have: -an abstract base class that will…
grybouilli
  • 185
  • 2
  • 11
0
votes
1 answer

Creating a friend function to access private variables from two classes

The problem was to access the private variables for output from two different classes by adding a function that is a friend of both classes. class Salesperson; class Sale { private: string date; double total; int…
0
votes
1 answer

How to organize classes containing mixture of templates and friends?

I have two C++ classes such that: The first class contains a pointer to the second class and has template function that calls second class's public method through a pointer. The function is defined already in the class declaration, for the reason…
MajinSaha
  • 188
  • 1
  • 9
0
votes
1 answer

Make a class template a friend of itself for different instantiation

I want to make A a friend of A for any types T and T2. Is this possible? Thanks. Test (also at godbolt.org): template class A { public: template void test(A& a) { a.v_ = 2;} private: int v_; template…
R zu
  • 2,034
  • 12
  • 30
0
votes
1 answer

Access private template with friend declaration

Class B uses a use a template, which is a private member of class A. I tried friend declaration but the template is still private within the context. How to allow B to use a private template of A? Test (also at godbolt.org): #include…
R zu
  • 2,034
  • 12
  • 30
0
votes
1 answer

Cannot use overloaded operator in friend function

I have the following code. In my .h file: #ifndef STRING_H #define STRING_H #include #include class String { private: char* arr; int length; int capacity; void copy(const String& other); void del(); …
nivalen292
  • 73
  • 8
0
votes
1 answer

operator overloading(using binaray friend function) class has no member, and member inaccessible

While doing a youtube tutorial on operator overloading, I'm having trouble fixing an operator overloading (using friend function) error message. The message received was about, class Complex has no member "operator+" and class "Complex::real"…
Rolando
  • 11
  • 4
0
votes
1 answer

Access private variable of class defined in header from class defined in source file

Header.h #pragma once namespace { class B; } namespace n1 { namespace n2 { class A { private: int i; public: friend class B; }; } } Source.cpp #include…
Swapnil
  • 1,424
  • 2
  • 19
  • 30