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

friend for private class

How to define friend for private classes? #include class Base_t{ private: struct Priv_t{ friend std::ostream & operator<<(std::ostream &os, const Priv_t& obj); } p; friend std::ostream &…
name
  • 5,095
  • 5
  • 27
  • 36
6
votes
2 answers

How do I define a friend class from the global namespace in another namespace?

In a previous Q&A (How do I define friends in global namespace within another C++ namespace?), the solution was given for making a friend function definition within a namespace that refers to a function in the global namespace. I have the same…
6
votes
1 answer

Inner class, pimpl and a friend class - disagreeing compilers

I was mucking about in some old library code, with the basic objective of refactoring it. This old code does not exactly comply to best practices and beauty (yes - friends are bad, and it has been removed after discovering the below - as it was an…
6
votes
1 answer

How to declare two classes such that A has members of B and B marks members of A as friends?

I am attempting to do exercise 7.32 from C++ Primer 5th Edition. That exercise asks the following: Define your own versions of Screen and Window_mgr in which clear is a member of Window_mgr and a friend of Screen. Here are the definitions for…
Adam
  • 73
  • 4
6
votes
1 answer

Forward Declaration of Template Function

I have a template class with a friend template function. I currently have the following code and it is working: template class Vector { public: template friend Vector operator*(const W lhs, const Vector&…
noddy
  • 3,827
  • 3
  • 24
  • 21
6
votes
2 answers

C++ Forward Declaration and Friendship in Namespace

According to 7.3.1.2 Namespace member definitions in C++ Standard ISO/IEC 14882:2003(E) Every name first declared in a namespace is a member of that namespace. If a friend declaration in a non-local class first declares a class or function…
6
votes
1 answer

Cannot understand friend functions in a template class

This is code i have written to understand the concept. The code is fine and it runs. What i dont understand is that why is the marked line needed ? template class D { public : template //<------------------Why is this…
asheeshr
  • 4,088
  • 6
  • 31
  • 50
6
votes
3 answers

friend functions of a class inside a namespace

I've two question about this code bellow: namespace A { class window; } void f(A::window); namespace A { class window { private: int a; friend void ::f(window); }; } void f(A::window rhs) { std::cout << rhs.a <<…
AlexDan
  • 3,203
  • 7
  • 30
  • 46
6
votes
4 answers

C++-like friend class mechanism in Java

Do you know how can I make an object changeable only inside a special class? In this example I want the object PrivateObject to be only changable (incrementable) inside the Box class, nowhere else. Is there a way to achieve this? public class Box {…
user1306777
  • 459
  • 1
  • 7
  • 13
6
votes
2 answers

Including header file in class that is a friend

I was wondering if you have to #include "Class1.h" in a class that is using that as a friend. For example the .h file for the class that is granting permission to Class1 class. class Class2 { friend class Class1; } would you need to #include…
JDN
  • 509
  • 3
  • 8
  • 14
6
votes
2 answers

Why can't I befriend a template parameter?

When researching an answer to a question (based on this answer) I tried to do the following: template class friendly { friend class T; }; friendly howdy; This fails to compile with the following error: error: template…
Motti
  • 110,860
  • 49
  • 189
  • 262
5
votes
2 answers

How to hide a datum from everyone but class T

I want a type A that will yield its hidden datum to an object of type T but hide the datum from everyone else. My C++ compiler happens to be GCC 4.4, but that shouldn't matter. Why won't this work? #include template class A…
thb
  • 13,796
  • 3
  • 40
  • 68
5
votes
0 answers

How many specializations of class template with not-type template argument must become friends?

In the following code, there is template struct A with not-type template argument, and class B declares A<0> as its friend. Then the member function A::has_access() is used to check that it has access to private B::p(): template struct A…
Fedor
  • 17,146
  • 13
  • 40
  • 131
5
votes
2 answers

restriction on friend function

According to the text I am referring to (The Complete Reference by Herbert Schildt) a derived class doesn't inherit friend functions and friend functions may not have a storage-class specifier. That is, they may not be declared as static or extern.…
haris
  • 2,003
  • 4
  • 25
  • 24
5
votes
1 answer

Why is a friend function not treated as a member of a namespace of a class it was declared in without additional declaration?

Suppose we have a class foo from a namespace space which declares a friend function named bar, which is later on defined, like so: namespace space { struct foo { friend void bar(foo); }; } namespace space { void bar(foo f) {…
Fureeish
  • 12,533
  • 4
  • 32
  • 62