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

Can a friend function in C++ have a default argument whose type has a private destructor?

In the next example the class U with private destructor has a friend function foo. And this friend function has argument of type U with default value U{}: class U{ ~U(); friend void foo(U); }; void foo(U = {}); Clang and MSVC accept this code, but…
Fedor
  • 17,146
  • 13
  • 40
  • 131
17
votes
5 answers

Using "friend"-declarations for unit testing. Bad idea?

[Of course, the question is not restricted to a specific "friend" implementation, feel free though to point out implementation specifics if relevant] Reading through the unanswered questions, I stumbled upon the InternalsVisibleTo…
David Schmitt
  • 58,259
  • 26
  • 121
  • 165
17
votes
2 answers

Why does the Standard prohibit friend declarations of partial specializations?

The C++ standard prohibits friend declarations of partial specializations. (§14.5.3/8): Friend declarations shall not declare partial specializations. [Example: template class A { }; class X { template friend class A; …
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
16
votes
4 answers

How do I make a template parameter's constructor a friend?

In C++11, they made it possible to friend a template parameter simply with friend T. You can also friend methods within that parameter with friend T::Method(). However, how do you friend a template parameter's constructor? class…
irfna
  • 525
  • 1
  • 4
  • 13
16
votes
4 answers

How do I define friends in global namespace within another C++ namespace?

I'd like to define a binary operator on in the global namespace. The operator works on a class that is defined in another namespace and the operator should get access to the private members of that class. The problem I have is that I don't know how…
Andreas
  • 844
  • 1
  • 8
  • 12
15
votes
3 answers

Declare a member-function of a forward-declared class as friend

Is it possible to declare a member function of a forward-declared class as friend? I am trying to do the following: class BigComplicatedClass; class Storage { int data_; public: int data() { return data_; } // OK, but provides too broad…
hrr
  • 1,807
  • 2
  • 21
  • 35
15
votes
3 answers

Does "friend"ing a class extend to classes declared within that class?

I have the following code where class A declares class B as friend. Should class C, declared within class B, be able to view private declarations/members of class A? It compiles without error with CL version 16 (Visual Studio 2010), but gcc g++…
Jack V.
  • 1,381
  • 6
  • 20
15
votes
4 answers

How to befriend private nested class

I thought I could do this: class TestA { private: class Nested { }; }; class TestB { public: friend class TestA; friend class TestA::Nested; }; But I get an error: Error C2248 'TestA::Nested': cannot access private class declared in…
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
15
votes
3 answers

C++ template friend operator overloading

What is wrong with my code? template class Float { friend Float operator+ (const Float &lhs, const Float &rhs); }; G++ just keeps warning: float.h:7: warning: friend declaration ‘Float operator+(const Float
Zifei Tong
  • 1,697
  • 1
  • 19
  • 32
15
votes
2 answers

Can a friend of A be also a friend of A>?

Consider the following code: #include template class Container; template Container> make_double_container(const std::vector>&); template class Container { std::vector
Alberto Santini
  • 6,425
  • 1
  • 26
  • 37
15
votes
1 answer

Does it make a difference whether I put 'friend class xxxxx' in the public or private section?

class A1 { public: friend class B; } class A2 { private: friend class B; } Any difference?
Kyle
  • 4,487
  • 3
  • 29
  • 45
15
votes
2 answers

C++ friend function can't access private members

This is supposed to be a string class with a bunch of operators and functions, including two friend functions. And those two cause some trouble for me, because the compiler says that they can not access the private members. Here is my…
spinakker
  • 293
  • 1
  • 2
  • 6
15
votes
5 answers

Virtual friend functions for a base class?

I'm in the proccess of learning the language and this is a noob doubt. Is it possible to use a virtual friend function? I don't know if it's possible, I didn't even test it but it could be useful in some situations. For example, for the overloaded…
Kurospidey
  • 393
  • 1
  • 4
  • 18
15
votes
1 answer

Depicting friend relationship between classes in UML

I have two classes A and B where B is a friend of A. How to show this in UML ? Is it ok to show it using dependency relation and then adding a comment on the relation like below ? <> B ------------------> A
Arun
  • 3,138
  • 4
  • 30
  • 41
14
votes
4 answers

When should you use friend classes?

Possible Duplicate: When should you use 'friend' in C++? I have come to a stumbling block because of lack of documentation on friend classes. Most books just explain it briefly, e.g an excerpt from C++: the Complete Reference: Friend Classes are…
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153