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
34
votes
5 answers

Why is it possible to place friend function definitions inside of a class definition?

Is it not supposed for a friend function to be explicitly defined outside of a class ? If so why can i declare a friend function inside a class definition just like any member function ? What is this ? Is it only OK with some operators such as <…
Hossein
  • 24,202
  • 35
  • 119
  • 224
32
votes
7 answers

How to propagate friend for derived classes

I want to have a class hierarchy and be able to create objects from it only inside a Factory. Example: class Base { protected: Base(){}; virtual void Init(){}; friend class Factory; }; class SomeClass : public Base { …
Mircea Ispas
  • 20,260
  • 32
  • 123
  • 211
30
votes
3 answers

Can we increase the re-usability of this key-oriented access-protection pattern?

Can we increase the re-usability for this key-oriented access-protection pattern: class SomeKey { friend class Foo; // more friends... ? SomeKey() {} // possibly non-copyable too }; class Bar { public: void…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
29
votes
2 answers

Befriending a template template parameter

I've got a class template with a template template parameter, and I want to declare this parameter (that is, all of its specializations) as a friend. But I can't find the correct syntax. template