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

a class-key must be declared when declaring a friend

The g++ compiler complains with this error when I declare a friend thusly: friend MyClass; instead of friend class MyClass; Why should the class keyword be required? (the Borland C++ compiler, BTW, does not require it.) Couldn't the compiler…
Roger Nelson
  • 1,882
  • 2
  • 15
  • 21
23
votes
7 answers

What is the equivalent of a 'friend' keyword in C Sharp?

What is the equivalent of a 'friend' keyword in C Sharp? How do I use the 'internal' keyword? I have read that 'internal' keyword is a replacement for 'friend' in C#. I am using a DLL in my C# project that I have the source code for and yet I do not…
xarzu
  • 8,657
  • 40
  • 108
  • 160
21
votes
2 answers

Why does the derived class have access to the private field of the base class?

class Base { public: class FirstBase { friend class Base; int x = 10; }; class SecondBase : public FirstBase { public: SecondBase() : FirstBase() {} void t() { std::cout << FirstBase::x; } }; }; This code compiles and…
Manarbek
  • 221
  • 5
21
votes
4 answers

"Friend"ing classes in python

Is there any way to make certain variables in classes "private" (or whatever self.__var really is) but be accessible to another class, like friends in c++, except in python? I do not want the variables in either class being messed with. Nor do I…
calccrypto
  • 8,583
  • 21
  • 68
  • 99
21
votes
5 answers

Friend scope in C++

If I have three classes, A, B, C. A and B are friends (bidirectionally). Also, B and C are friends (bidirectionally). A has a pointer to B and B has a pointer to C. Why can't A access C's private data through the pointer? Just to clarify: This is a…
Marcin
  • 12,245
  • 9
  • 42
  • 49
20
votes
9 answers

In C# 4.0, is there any way to make an otherwise private member of one class available only to a specific other class?

We're creating an object hierarchy where each item has a collection of other items, and each item also has a Parent property pointing to its parent item. Pretty standard stuff. We also have an ItemsCollection class that inherits from…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
18
votes
5 answers

C++ - must friend functions be defined in the header file?

I want to overload the operator << in one of my classes. The signature goes like this: friend std::ostream& operator<<(std::ostream& os, const Annuaire& obj) When I try to define it in the .cpp file it says that the operator<< exactly takes 1…
Pacane
  • 20,273
  • 18
  • 60
  • 97
18
votes
4 answers

How to name this key-oriented access-protection pattern?

Apparently this key-oriented access-protection pattern: class SomeKey { friend class Foo; SomeKey() {} // possibly non-copyable too }; class Bar { public: void protectedMethod(SomeKey); // only friends of SomeKey have…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
18
votes
1 answer

some friend functions don't follow the rule

For the following snippet: class A{ friend void f(){}; public: A(){f();} //error }; class B{ friend void f(void* ptr){}; public: B(){f(this);} //no error }; According to the rule that although friend functions can…
Eddie Deng
  • 1,399
  • 1
  • 18
  • 30
18
votes
1 answer

Friend class not working

I am getting the typical '... is private within this context' error. Can you tell me what I am doing wrong? Code is shortened for readability. in class SceneEditorWidgetController: (settingsdialog and the variable used here is defined in the…
RunOrVeith
  • 4,487
  • 4
  • 32
  • 50
18
votes
2 answers

VB.NET: what does the 'friend' modifier do?

What does the 'friend' modifier do in VB.NET? Why is it the default modifier for GUI components in Visual Studio?
CJ7
  • 22,579
  • 65
  • 193
  • 321
18
votes
10 answers

How does the friend keyword (Class/Function) break encapsulation in C++?

Some programmer said that, "a friend function break the encapsulation in C++". and some programmer also said, "Friend functions do not break encapsulation; instead they naturally extend the encapsulation barrier" what does it mean?.. If a friend…
Chris
17
votes
3 answers

How to allow template function to have friend(-like) access?

How does one modify the following code to allow template function ask_runUI() to use s_EOF without making s_EOF public? #include #include #include #include class AskBase { protected: std::string m_prompt; …
CW Holeman II
  • 4,661
  • 7
  • 41
  • 72
17
votes
1 answer

friendship with extern "C" function seems to require :: to qualify name

Trying to make a class friends with an extern "C" function, this code works: #include extern "C" { void foo(); } namespace { struct bar { // without :: this refuses to compile friend void ::foo(); bar() : v(666) {} …
Flexo
  • 87,323
  • 22
  • 191
  • 272
17
votes
2 answers

Template specializations in friend declarations post C++20

Context: To our surprise, MSVC with C++20 mode and two-phase compliance enabled accepts the following code: template class X { friend int foo(X x); int a = 10; }; template int foo(T t) { return t.a; } int…
Max Langhof
  • 23,383
  • 5
  • 39
  • 72