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
12
votes
2 answers

C++: Correct syntax for friending a template type member of template parameter?

I have a class that takes a template type parameter (tTRAIT). I want to friend a template type member alias of tTRAIT, but I can't figure out the syntax. (Is this even possible?). template struct SFoo {}; struct STrait { …
xaxazak
  • 748
  • 4
  • 16
12
votes
1 answer

Friend Assemblies in C#

I'm trying to create some 'friend assemblies' using the [InternalsVisibleTo()] attribute, but I can't seem to get it working. I've followed Microsoft's instructions for creating signed friend assemblies and I can't see where I'm going wrong. So I'll…
Tim Long
  • 13,508
  • 19
  • 79
  • 147
12
votes
1 answer

What's the difference between "friend struct A;" and "friend A;" syntax?

What is the difference between doing: struct A; struct B { friend struct A; }; and struct A; struct B { friend A; }; What does it mean to leave out struct in the second part?
Walter
  • 123
  • 1
  • 1
  • 4
12
votes
2 answers

Template class friendship

I recently came across a c++ piece of code where a class is made friend to itself. As I have read on different forums a class is already a friend to itself. So I was wondering if there is a specific reason why one would want to make a class friend…
AleC
  • 579
  • 3
  • 7
  • 15
12
votes
2 answers

Compiler error in declaring template friend class within a template class

I have been trying to implement my own linked list class for didactic purposes. I specified the "List" class as friend inside the Iterator declaration, but it doesn't seem to compile. These are the interfaces of the 3 classes I've…
Davide Valdo
  • 779
  • 8
  • 21
12
votes
6 answers

Overload operators as member function or non-member (friend) function?

I am currently creating a utility class that will have overloaded operators in it. What are the pros and cons of either making them member or non-member (friend) functions? Or does it matter at all? Maybe there is a best practice for this?
Seth
  • 8,213
  • 14
  • 71
  • 103
12
votes
8 answers

friend class with inheritance

If I have two Classes as follows with inheritance: class A { ... } class B : public A { ... } And a third class with defined as a friend class A: class C { friend class A; } Will I be able to access from class B (which is also an…
Harry
  • 1,362
  • 12
  • 19
11
votes
2 answers

PHP friend/package visibility

Is there any way to limit the visibility in PHP in the same way as "package" visibility works in Java or at least "friend" visibility in C++? What's the best practice to maintain large OOP project and not to let anyone use any part of code? I use…
Pavel S.
  • 11,892
  • 18
  • 75
  • 113
11
votes
1 answer

Why can’t protected members be used by friends of derived classes?

The C++ standard states in [class.access/1] (emphasis mine): A member of a class can be private; that is, its name can be used only by members and friends of the class in which it is declared. protected; that is, its name can be used only by…
Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
11
votes
2 answers

Hidden friends: declarations and definitions

In his recent blog post Anthony Williams talks about hidden friends. The main idea, if I understood it correctly, is that functions declared as friends cannot be found by ADL in certain situations. Simple example: namespace N { struct A { …
Evg
  • 25,259
  • 5
  • 41
  • 83
11
votes
3 answers

Inaccessible base class despite friendship

There are tons of questions regarding this error and all of the answers seem to imply that downcasting is impossible. Only this answer mentions friendship as possible solution, at least as I understand it. However the following code (irrelevant…
Maple
  • 229
  • 2
  • 12
11
votes
1 answer

What is the point of the complicated scoping rules for friend declarations?

I recently discovered that friend declarations scoping follows extremely peculiar rules - if you have a friend declaration (definition) for a function or a class that is not already declared, it is automatically declared (defined) in the immediately…
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
11
votes
2 answers

Friend access to protected nested class

I have the following C++ code: class A { protected: struct Nested { int x; }; }; class B: public A { friend class C; }; class C { void m1() { B::Nested n; // or A::Nested } }; Compiling this snippet with g++ 4.4, it does not…
Torsten Marek
  • 83,780
  • 21
  • 91
  • 98
11
votes
2 answers

How do you mark a struct template as friend?

I have code like this: template struct MyStruct { T aType; U anotherType; }; class IWantToBeFriendsWithMyStruct { friend struct MyStruct; //what is the correct syntax here ? }; What is the correct syntax to…
David
  • 9,635
  • 5
  • 62
  • 68
11
votes
1 answer

How to friend a specific template specialization?

Is there a syntax for friending only certain specializations of a templated class, or do you have to friend all specializations with the likes of: template friend class Bar; If it existed, I would be looking for something…