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

Specific Template Friendship in C++

I have a question for Specific Template Friendship in C++. in the book C++ Primer, the specific template friendship is written like this: template class Foo3; template void templ_fcn3(const T&); template class…
Tianyi
  • 197
  • 1
  • 8
7
votes
4 answers

How to make a class able to access only certain private members of another class?

Suppose we have two classes: class Base { private: int x; public: void f(); }; class Foo { // some variables and methods }; Now everyone can call Base::f(), but I want only Foo to be able to do so. In order to achieve this effect, we…
miloszmaki
  • 1,635
  • 15
  • 21
7
votes
2 answers

Inheritance and Friendship access. C++

I have the following query; classB inherits from classA classC is friend of classB Doesn't this mean classC should be able to access protected member of classA? Since classB inherits this from classA, an classC can access everything in class…
user1005253
  • 404
  • 2
  • 7
  • 21
6
votes
3 answers

Does a friend see base classes?

Given the sample code: class Base { public: bool pub; protected: bool prot; }; class Derived : private Base { friend class MyFriend; }; class MyFriend { Derived _derived; void test() { // Does standard provide me access to…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
6
votes
2 answers

How do I make main a friend of my class?

I'm thinking this is possible, but the compiler is complaining it cannot access the protected/private members of my class. I've tried moving stuff around and changing signatures, but can't find a combination that works. I essentially have: class…
Jaime
  • 1,182
  • 2
  • 12
  • 29
6
votes
4 answers

friend function of std::make_shared() in Visual Studio 2010 (not Boost)

how to make friend function of std::make_shared(). I tried: class MyClass{ public: friend std::shared_ptr std::make_shared(); //or //friend std::shared_ptr std::make_shared(); protected: …
uray
  • 11,254
  • 13
  • 54
  • 74
6
votes
2 answers

I have multiple overloads of the * operator in a template class. Why do I get different results when I put the declarations in different order?

I'm having a class which needs to have multiple overloads of the * operator. Some of these need to be declared as friends so I can have the class type as second argument. This is an example of a class which encounters the problem I'm about to…
6
votes
3 answers

How do I unit test private functions from a separate project in VB .NET?

As I develop code, I often want to unit test some of the building blocks of a class even if they are normally private. If my unit tests are inside the project, I can use "Friend" to accomplish this and still keep the functions private for normal…
user78369
  • 61
  • 1
  • 2
6
votes
4 answers

Pimpl idiom and internal object collaboration without friend declaration

I'm implementing several classes using the pimpl idiom and am coming across some design issues. Firstly, I've always seen pimpl done like this class Object { public: Visible(); ~Visible(); .. etc .. private: class ObjectImpl…
ScaryAardvark
  • 2,855
  • 4
  • 30
  • 43
6
votes
1 answer

Friend, private function, template alias, and decltype... is clang correct in rejecting this?

In the following code (godbolt link): #include struct Friend { class Inner { friend struct Friend; int function() { return 0; } }; using DirectResult = decltype(std::declval().function()); …
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
6
votes
1 answer

A question about name lookup with friend function

I have read the standard section of [basic.lookup.unqual] and I am confused about this: typedef int f; namespace N { struct A { friend void f(A &); operator int(); void g(A a) { int i = f(a); // f is the typedef, not the…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
6
votes
1 answer

C++ Template friend odd behavior

I'm seeing something I can't explain in the following code. Under VS6, VS9, and GCC T2::foo2() gives the error: 'bar' : cannot access protected member declared in class 'C1'. But if you remove C1::bar(), it compiles and runs correctly, even though…
Greg
  • 63
  • 3
6
votes
2 answers

C++ inline definition of friend function

In the current draft of the C++ standard (march 2019) [class.friend] p.6 states (emphasis mine): A function can be defined in a friend declaration of a class if and only if the class is a non-local class ([class.local]), the function name is…
user42768
  • 1,951
  • 11
  • 22
6
votes
4 answers

Opposite of friend declaration

Say we have a class that has a private constructor, through friend we can allow some specific class(es) to still create objects of this class: class Foo { friend class Bar; private: Foo(); }; class Bar { Bar() { //create a Foo object …
Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
6
votes
2 answers

Restrict the scope of class instances accessible by multiple template parameter friend function

I would like to know if what I am aiming for is possible. I have a class Class such that #include template class Class; template Class f(Class& C, const Class& D); template class Class…
user9063318