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

Why does a C++ friend class need a forward declaration only in other namespaces?

Suppose I have a class F that should be friend to the classes G (in the global namespace) and C (in namespace A). to be friend to A::C, F must be forward declared. to be friend to G, no forward declaration of F is necessary. likewise, a class A::BF…
pesche
  • 3,054
  • 4
  • 34
  • 35
56
votes
4 answers

Is this key-oriented access-protection pattern a known idiom?

Matthieu M. brought up a pattern for access-protection in this answer that i'd seen before, but never conciously considered a pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly make it non-copyable too }; class Bar…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
54
votes
5 answers

Is there any way to access private fields of a struct from another package?

I have a struct in one package that has private fields: package foo type Foo struct { x int y *Foo } And another package (for example, a white-box testing package) needs access to them: package bar import "../foo" func change_foo(f *Foo)…
Matt
  • 21,026
  • 18
  • 63
  • 115
52
votes
4 answers

C++ friend inheritance?

Does a subclass inherit, the main class' friend associations (both the main class' own and other classes friended with the main class)? Or to put it differently, how does inheritance apply to the friend keyword? To expand: And if not, is there any…
SE Does Not Like Dissent
  • 1,767
  • 3
  • 16
  • 36
49
votes
3 answers

Making a template parameter a friend?

Example: template class Base { public: Base(); friend class T; }; Now this doesn't work... Is there a way of doing this? I'm actually trying to make a general class sealer like this: class ClassSealer { private: friend class…
Jonas
  • 1,532
  • 3
  • 16
  • 20
46
votes
3 answers

'friend' functions and << operator overloading: What is the proper way to overload an operator for a class?

In a project I'm working on, I have a Score class, defined below in score.h. I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is printed. Here's what I tried to do: ostream & Score::operator<< (ostream &…
F. P.
  • 5,018
  • 10
  • 55
  • 80
46
votes
5 answers

ampersand (&) at the end of variable etc

I am a C++ noob and i've a problem of understanding c++ syntax in a code. Now I am quite confused. class date { private: int day, month, year; int correct_date( void ); public: void set_date( int d, int m, int y ); void actual( void ); void print(…
snipor
  • 461
  • 1
  • 5
  • 5
45
votes
2 answers

What is the fully qualified name of a friend function defined inside of a class?

What is the fully qualified name of a friend function defined inside of a class? I recently saw an example analogous to the following. What is the fully qualified name of val() below? #include namespace foo { class A { int…
Szabolcs
  • 24,728
  • 9
  • 85
  • 174
43
votes
2 answers

declare template friend function of template class

I have a class template Obj and a function template make_obj. Obj has a private single constructor defined, which takes a reference to its templated type to bind to. template class Obj { private: T& t; Obj(T& t) :…
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
42
votes
2 answers

Access friend function defined in class

There is such code: #include class A{ public: friend void fun(A a){std::cout << "Im here" << std::endl;} friend void fun2(){ std::cout << "Im here2" << std::endl; } friend void fun3(); }; void fun3(){ std::cout << "Im…
scdmb
  • 15,091
  • 21
  • 85
  • 128
42
votes
3 answers

Is it possible to declare a friend function as static?

Here is some C++ example code that compiles and works fine: class A { public: A() {/* empty */} private: friend void IncrementValue(A &); int value; }; void IncrementValue(A & a) { a.value++; } int main(int, char **) { A a; …
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
42
votes
9 answers

Allowing a "friend" class to access only some private members

Suppose I have three C++ classes FooA, FooB and FooC. FooA has an member function named Hello, I want to call this function in class FooB, but I don't want class FooC be able to call it. The best way I can figure out to realize this is to declare…
Archer
  • 592
  • 1
  • 4
  • 11
37
votes
6 answers

What's the scope of inline friend functions?

After searching aroung SO, one question taught me that the lexical scope of an inline friend function is the class it's defined in, meaning it can access e.g. the typedefs in the class without qualifying them. But then I wondered what is the actual…
Xeo
  • 129,499
  • 52
  • 291
  • 397
37
votes
11 answers

When to use friend class in C++

Possible Duplicate: When should you use 'friend' in C++? I was brushing up on my C++ (I'm a Java developer) and I came across the friend class keyword which I had forgotten about for a while. Is this one of those features that's just part of the…
GBa
  • 17,509
  • 15
  • 49
  • 67
37
votes
5 answers

PHP equivalent of friend or internal

Is there some equivalent of "friend" or "internal" in php? If not, is there any pattern to follow to achieve this behavior? Edit: Sorry, but standard Php isn't what I'm looking for. I'm looking for something along the lines of what ringmaster…
smack0007
  • 11,016
  • 7
  • 41
  • 48