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
14
votes
3 answers

Is friendship inherited in C++?

Suppose I have a Base class: class Base { friend SomeOtherClass; }; And there is another (different) class that inherits from Base: class AnotherClass : public Base {} Is friendship inherited as well?
tunnuz
  • 23,338
  • 31
  • 90
  • 128
14
votes
3 answers

c++ friend function - operator overloading istream >>

My question is in regards to friend functions as well as overloading the << and >>. From my understanding I thought friend functions could (and should) access private member variables directly. However in the case I have here the compiler would only…
user399415
  • 143
  • 1
  • 1
  • 5
14
votes
1 answer

I believe clang erroneously allows inline friend function access to data in an enclosing scope. Both gcc and vs2013 reject this code

The friend function f doesn't have access to a private member of the enclosing class A. #include class A{ const static int p = 1; class B { friend void f() { std::cout << p << '\n'; std::cout << q…
Belloc
  • 6,318
  • 3
  • 22
  • 52
14
votes
1 answer

Friend lookup exception from template-id?

Consider the following clause in [namespace.memdef]/3: If the name in a friend declaration is neither qualified nor a template-id and the declaration is a function or an elaborated-type-specifier, the lookup to determine whether the entity has…
Barry
  • 286,269
  • 29
  • 621
  • 977
14
votes
4 answers

Friend function is unable to construct a unique pointer of the class

I have a certain design strategy where the constructor of my class is private and can only be constructed by friends of the class. Inside the friend function, I am trying to create a unique_pointer of my class using std::make_uniquebut it doesn't…
Abhijit
  • 62,056
  • 18
  • 131
  • 204
14
votes
2 answers

How can I remove/refactor a «friend» dependency declaration properly?

The background of this question is based on a practical sample where I wanted to remove a «friend» dependency from a pair of classes that are used to manage read/write locked access to a shared resource. Here's an abstraction of the original…
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
14
votes
3 answers

Declaring main as friend considered harmful?

Discussion I know that main can be a friend of a class: #include class foo { friend int main(); int i = 4; }; int main() { foo obj; std::cout << obj.i << std::endl; } LIVE DEMO However, I feel that although this is perfectably…
101010
  • 41,839
  • 11
  • 94
  • 168
14
votes
3 answers

class friend function inside a namespace

Im trying to define a class friend function outside the namespace like this: namespace A{ class window{ private: int a; friend void f(window); }; } void f(A::window rhs){ cout << rhs.a << endl; } Im getting an error said that there…
AlexDan
  • 3,203
  • 7
  • 30
  • 46
13
votes
2 answers

Is it possible to mark an alias template as a friend?

Imagine we have this code: template class Element {}; template class Util { public: template using BeFriend = Element; }; Is it possible to mark BeFriend as a friend ? (Of Util, or any other…
Ad N
  • 7,930
  • 6
  • 36
  • 80
13
votes
3 answers

How to make the lambda a friend of a class?

Let's say, I have a class: class A { int a; }; And I have a lambda: auto function = [](A* a) { a->a; // <== gives an error in this line. }; function(new A); Is there any way to use a private member/method inside a lambda? - It's not…
abyss.7
  • 13,882
  • 11
  • 56
  • 100
13
votes
2 answers

friend class declaration and using directive

Is the following example well-formed? namespace N { class A; } using namespace N; class B { int i; friend class A; }; namespace N { class A { B m; int get() { return m.i; } }; } This example compiled…
Mitsuru Kariya
  • 938
  • 4
  • 14
13
votes
4 answers

C++ How to specify all friends of a templated class with a default argument?

To define a friend of a templated class with a default argument, do you need to specify all friends as in the code below (which works)? // Different class implementations enum ClassImplType { CIT_CHECK, CIT_FAST, CIT_GPU, CIT_SSE, CIT_NOF_TYPES }…
Anne van Rossum
  • 3,091
  • 1
  • 35
  • 39
12
votes
1 answer

Granting friendship to constructor/destructor of template class specialization - works under C++17, but fails under C++20

I have found a situation where code compiles successfully under C++17, but FAILS under C++20. This blocks us from upgrading our existing code design to the newer standard. Why doesn't this compile under C++20? It seems like a weird breach of…
Giffyguy
  • 20,378
  • 34
  • 97
  • 168
12
votes
5 answers

Can a friend class object access base class private members on a derived class object?

I'm surprised that the code below compiles. It seems that a class befriended to the (publicly inherited) base class can access a member of the base class provided an instance of the derived class. If the inheritance is changed to private then…
Hemant Sharma
  • 141
  • 1
  • 6
12
votes
2 answers

The member function Outer::f() is not a friend of class Outer::Inner. Why?

According to clang, gcc and vs2013, the function Outer::f is not a friend of the class Outer::Inner. struct Outer { void f() {} class Inner { friend void f(); static const int i = 0; }; }; void f() { int i =…
Belloc
  • 6,318
  • 3
  • 22
  • 52