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
8
votes
1 answer

Placement of friend declarations

Does it matter where in a class a friend clause is placed (i.e. within the protected block as opposed to the private block)?
Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
7
votes
1 answer

What is the difference of friend iterator and friend class iterator which encounter in thinking in c++?

In Thinking in C++ Volume 1, chapter 16: Introduction to Templates. The context: Notice that instead of just saying: friend iterator; // Make it a friend This code has: friend class iterator; // Make it a friend This is important because…
Alister
  • 71
  • 1
7
votes
2 answers

C++ Private Nested Abstract Class

So maybe this is a dumb question and I'm over thinking this, but I have the following situation. I am making a "class Shell" which can run abstract "class Action" objects. It is the only class that should create or use these objects. Action…
Matt
  • 5,478
  • 9
  • 56
  • 95
7
votes
6 answers

Friending a template parameter

It's impossible to friend a template parameter because the standard disallows it. How might I get effectively the same thing then? What I want is basically a type that is unusable outside the object which owns it. Why is rather beside the point…
Edward Strange
  • 40,307
  • 7
  • 73
  • 125
7
votes
1 answer

Why can't I inline-define a non-templated friend within a templated class?

MCVE's speak louder than words: // int bar(); template class Foo { friend int ::bar() { return 123; } }; int main() { Foo f1; Foo f2; } with GCC 6 and --std=c++14, this gives me: a.cpp: In instantiation of ‘class…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
7
votes
1 answer

How to define a friend function declared in a non template class internal to a template class outside of both classes?

I found "how to define a friend template function of a template class outside of its declaration" (SO/cppreference), but how to do that if we add another internal non template class in the mix? I.e. how to (externally) define operator<< declared in…
Dev Null
  • 4,731
  • 1
  • 30
  • 46
7
votes
2 answers

How to simulate C++ friend in C# and VB.NET?

Because sometimes, I really need a friend. I can think of the following tricks: Read only wrapper - like ReadOnlyCollection. The friend keeps the pointer to the modifiable object, while everyone else can access only the wrapper. Write delegate -…
Ansis Māliņš
  • 1,684
  • 15
  • 35
7
votes
2 answers

Making a member function a friend

What happens when you make a member function of a class a friend of itself!? The code below compiles and runs. Without the friend declaration a 'too many arguments to operator' is generated (and rightly so). I realise that doing this doesn't make…
radman
  • 17,675
  • 11
  • 42
  • 58
7
votes
1 answer

using-declaration for friend function

In C++11 it is possible to make a public member of a private base class accessible to the outside (public) with a using declaration. For example class A { private: int i = 2; public: void f() { i = 3; } friend bool operator==(const A&…
tmlen
  • 8,533
  • 5
  • 31
  • 84
7
votes
2 answers

Do I really need to bend over backwards for a friend operator<< for a class in a namespace?

I want to implemnent an operator<< for streaming my class (say it's named Paragraph). Class Paragraph has some private data, for which reason I want the (freestanding) operator<< function to be a friend. So, I do as suggested, e.g., here on SO.…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
7
votes
1 answer

Return type match with auto and friend function

So I was answering this question: Define friend function template of class template, and I found some "weird" behavior from g++ (5.3) and clang (3.8): Let's assume the following template: template struct test { private: int value; …
Holt
  • 36,600
  • 7
  • 92
  • 139
7
votes
2 answers

Friend function cannot access private function if class is under a namespace

I have a class inside a namespace and that class contains a private function. And there is a global function. I want that global function to be the friend of my class which is inside the namespace. But when I make it as a friend, the compiler thinks…
user389679
  • 71
  • 1
  • 2
7
votes
2 answers

In C++, does adding a friend to a class change its memory layout?

Also, does it matter where in the class you declare the friend ? Does it matter if you add a friend class or a friend function ?
Naveen
  • 5,910
  • 5
  • 30
  • 38
7
votes
1 answer

Does access specifier matters for a friend function?

In a class, if the function is declared as friend within the different specifier like - private, protected, or public, then is there any difference. As per my understanding, friend function is not a member. Thus, it shouldn't matter. But, if I see…
dexterous
  • 6,422
  • 12
  • 51
  • 99
7
votes
1 answer

Decltype and friend functions in Visual Studio vs G++

I was writing some C++ code to do vector math. It is essential just a thin wrapper around a std::array instance. I wanted to overload the non-member begin() function to return an iterator to the beginning of the backing array. To do this, I wrote a…
David Adrian
  • 1,079
  • 2
  • 9
  • 24