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

Is it possible to manually set istream fail bit in C++11

I've made this class: class object { // data... public: friend std::istream& operator>>(std::istream& in, object& o) { char c, d; in >> c >> d; if (c == d) { /*set the fail bit some how*/ in.putback(d); …
haelmic
  • 541
  • 4
  • 18
8
votes
1 answer

Is a friend function defined in-class automatically inline?

If a member function is defined inside the class, it is an inline function. E.g. struct X { void mem_f() {} //mem_f is inline }; My question is whether a nonmember friend function defined inside the class is also automatically inline. …
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
8
votes
1 answer

C++ concept with friend-like access

Is it possible to make this code work as I'd like? I.e. to allow the concept to have access to a private member funcion? template concept bool Writeable() { return requires (T x,std::ostream os) { { x.Write(os) } -> void };…
metalfox
  • 6,301
  • 1
  • 21
  • 43
8
votes
2 answers

Templated Class Friend Operator Member Function

I'm trying to get a friend function inside a templated class to compile, but the error message and warning I do not understand. I've made a demonstration of the issue. The error I'm getting is: prog.cpp:8:57: error: non-class, non-variable partial…
JadziaMD
  • 2,690
  • 5
  • 31
  • 42
8
votes
1 answer

template object's template friend functions and namespaces

In the following C++ example code, GCC 6 and Clang 3.8 disagree on what the correct behaviour is: This contrived example "works" -- as in the test() function returns o.p in GCC. In clang, it calls the (undefined) function get
Matt Godbolt
  • 1,381
  • 11
  • 14
8
votes
3 answers

Template friendship error compilation with GCC but not with clang

This code compiles with clang 3.7.1 (with no diagnostic) but fails with GCC 5.3.0 (live example): #include template struct A { void foo() { static_cast(this)->implementation(); } }; struct Crtp :…
Paolo M
  • 12,403
  • 6
  • 52
  • 73
8
votes
3 answers

How to define friends for classes defined inside template class

Suppose I have the following template class that defines a nested class: template struct foo { struct bar { }; }; Suppose the environment I'm coding in also has the following helper class, which should be specialized for any type…
Ryan
  • 731
  • 6
  • 17
8
votes
1 answer

Is there a way to specify all classes in a variadic parameter pack to be friend of the template in order to use operator=?

I have seen a CRTP solution, which extracted the interface into the base class, and friended only one of the pack arguments per base class. Then the most derived class inherited all the friended base classes and implemented the interface. I cannot…
LoPiTaL
  • 2,495
  • 16
  • 23
8
votes
1 answer

Replacement for FBFriendPickerViewController for Facebook iOS SDK 4

According to Facebook v4 changelog, all FB*ViewController were deprecated and we should build our own table view controller to show friends list. Now, before I put myself working on it, does anyone knows an alternative for…
estemendoza
  • 3,023
  • 5
  • 31
  • 51
8
votes
2 answers

Must the C++ standard library support classes that are picky about who their friends are?

This question is easiest to illustrate with an example, so here goes: Is code like the following guaranteed to be valid, and compile & run correctly? (Not all implementations actually compile it correctly, but I'm wondering if that's a…
user541686
  • 205,094
  • 128
  • 528
  • 886
8
votes
0 answers

Partial specialization friend declaration

In the following code: template class A {}; template class B {}; template class C { template friend class A; // Works fine. // template
prestokeys
  • 4,817
  • 3
  • 20
  • 43
8
votes
1 answer

Using variadic templates to specify friend classes

I'm trying to use variadic templates to specify friend classes. I try with the following syntax, but it doesn't work. template struct A { friend Args...; }; I try to code some workarounds, but it seems to be not so simple since…
mattia.penati
  • 532
  • 5
  • 18
8
votes
1 answer

Overloading Output operator for a class template in a namespace

I've this program #include #include #include #include #include using namespace std ; #if 0 namespace skg { template struct Triplet ; } template ostream& operator<<…
Surya
  • 1,139
  • 1
  • 11
  • 30
8
votes
6 answers

C# equivalent to C++ friend keyword?

I am new to C#, and I have a problem for which in C++ I would normally use the friend identifier. Now I know the friend keyword doesn't exist in C#, but I don't have any experience with how to work around this (except for making all class variables…
Yellow
  • 3,955
  • 6
  • 45
  • 74
8
votes
1 answer

Unit test a class declared friend (internal)

In some of my test helper code, I have a IDbSet(Of T) implementation called FakeDbSet(Of T) that mocks a lot of EF behavior without an actual database. I have the class declared Friend because I want to force all code to interact with it like an…
just.another.programmer
  • 8,579
  • 8
  • 51
  • 90