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
394
votes
31 answers

When should you use 'friend' in C++?

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language. What is a good example of using friend? Reading the FAQ a bit longer I like…
roo
  • 7,106
  • 8
  • 39
  • 45
237
votes
18 answers

Is there a way to simulate the C++ 'friend' concept in Java?

I would like to be able to write a Java class in one package which can access non-public methods of a class in another package without having to make it a subclass of the other class. Is this possible?
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
223
votes
5 answers

What is the C# equivalent of friend?

Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like the private member variables of a class to be accessible to a Tester class without exposing them to other classes. In C++ I'd just declare the Tester class as a…
Ben L
  • 6,618
  • 8
  • 39
  • 34
223
votes
22 answers

Why does C# not provide the C++ style 'friend' keyword?

The C++ friend keyword allows a class A to designate class B as its friend. This allows Class B to access the private/protected members of class A. I've never read anything as to why this was left out of C# (and VB.NET). Most answers to this…
Ash
  • 60,973
  • 31
  • 151
  • 169
213
votes
2 answers

public friend swap member function

In the beautiful answer to the copy-and-swap-idiom there is a piece of code I need a bit of help: class dumb_array { public: // ... friend void swap(dumb_array& first, dumb_array& second) // nothrow { using std::swap; …
towi
  • 21,587
  • 28
  • 106
  • 187
176
votes
4 answers

Friend declaration in C++ - difference between public and private

Is there a difference between declaring a friend function/class as private or public? I can't seem to find anything about this online. I mean the difference between: class A { public: friend class B; }; and class A { private: //or nothing…
BIU
  • 2,340
  • 3
  • 17
  • 23
100
votes
9 answers

Why does C++ not allow inherited friendship?

Why is friendship not at least optionally inheritable in C++? I understand transitivity and reflexivity being forbidden for obvious reasons (I say this only to head off simple FAQ quote answers), but the lack of something along the lines of virtual…
Jeff
  • 3,475
  • 4
  • 26
  • 35
85
votes
5 answers

Are inner classes in C++ automatically friends?

If I define an inner class in C++, is it automatically a friend of the class that contains it? For example, is this legal: class Outer { public: class Inner { public: void mutateOuter(Outer& o); }; private: int…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
84
votes
5 answers

overloading friend operator<< for template class

I have read couple of the questions regarding my problem on StackOverflow.com now, and none of it seems to solve my problem. Or I maybe have done it wrong... The overloaded << works if I make it into an inline function. But how do I make it work in…
starcorn
  • 8,261
  • 23
  • 83
  • 124
72
votes
7 answers

What is wrong with making a unit test a friend of the class it is testing?

In C++, I have often made a unit test class a friend of the class I am testing. I do this because I sometimes feel the need to write a unit test for a private method, or maybe I want access to some private member so I can more easily setup the state…
cchampion
  • 7,607
  • 11
  • 41
  • 51
70
votes
1 answer

How to declare a templated struct/class as a friend?

I'd like to do the following: template struct foo { template friend struct foo; private: // ... }; but my compiler (VC8) chokes on it: error C3857: 'foo': multiple template parameter lists are not…
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
66
votes
4 answers

Error with multiple definitions of function

I am trying to relearn C++ after taking an intro course a few years ago and I’m having some basic problems. My current problem occurs when trying to use a friend function. Here is my code in 2 files. First: // fun.cpp #include using…
Adad Dayos
  • 701
  • 1
  • 5
  • 9
61
votes
5 answers

friend declaration declares a non-template function

I have a base Class akin to the code below. I'm attempting to overload << to use with cout. However, g++ is saying: base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base*)’ declares a non-template…
mike_b
  • 2,127
  • 5
  • 20
  • 25
60
votes
6 answers

clean C++ granular friend equivalent? (Answer: Attorney-Client Idiom)

Why does C++ have public members that anyone can call and friend declarations that expose all private members to given foreign classes or methods but offer no syntax to expose particular members to given callers? I want to express interfaces with…
Jeff
  • 3,475
  • 4
  • 26
  • 35
58
votes
4 answers

How to make google-test classes friends with my classes?

I heard there is a possibility to enable google-test TestCase classes friends to my classes, thus enabling tests to access my private/protected members. How to accomplish that?
pajton
  • 15,828
  • 8
  • 54
  • 65
1
2 3
99 100