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
9
votes
4 answers

Making default constructor private in Qt custom object

I'm reading this Qt doc page about custom types, and it states the following: The default constructor, copy constructor and destructor are all required, and must be public, if the type is to be integrated into the meta-object system. Suppose I…
ABCplus
  • 3,981
  • 3
  • 27
  • 43
9
votes
1 answer

Template friend function and return type deduction

Note: This question is really close to Return type deduction for in-class friend functions, but I did not find the answer to my problem there. Tested with clang 3.4 with std=c++1y and clang 3.5 with std=c++14 and std=c++1z This code…
Bérenger
  • 2,678
  • 2
  • 21
  • 42
9
votes
3 answers

C++11 Declaring factory a friend of base class

I'm trying to create a factory for derived classes. I only want the factory to be able to create instances of the derived classes so I've made the base constructor protected; the derived classes just use the base class constructors so their…
jlconlin
  • 14,206
  • 22
  • 72
  • 105
9
votes
3 answers

Is a local class in a method of a class a friend of this class?

I have an outer class A. It has a method A::fun. In this method, it has a local or inner class B. My question is: Is B a friend of A? I think it is not. Is it right? If so, I think let class B a friend of A is very beneficial since B can access to…
user1899020
  • 13,167
  • 21
  • 79
  • 154
9
votes
3 answers

Friend function from a templated class

I have a class like this: #include "Blarg.h" // ... class Foo : public Bar { // ... static double m_value; // ... }; And another one like this: template class Blarg : public Bar { // ... void SetValue(double…
norman
  • 5,128
  • 13
  • 44
  • 75
9
votes
0 answers

Partial specialization and friendship

Suppose you have class A like this: template class A; And class B like this: template class B; And now you want both classes be friends when T is the same type, is this possible? So for example A is…
user3624760
9
votes
1 answer

Class friendship - a puzzle

I am an object-oriented programming enthusiast at a beginner level. I have encountered the following puzzle: class A { }; class B { protected: friend class A; }; class C { public: friend class B; }; Referring to the sample code…
Prz3m3k
  • 605
  • 6
  • 14
8
votes
3 answers

Friendship not inherited - what are the alternatives?

I have written/am writing a piece of physics analysis code, initially for myself, that will now hopefully be used and extended by a small group of physicists. None of us are C++ gurus. I have put together a small framework that abstracts the…
Anthony
  • 89
  • 2
8
votes
3 answers

Template parameter as a friend

In C++03 the following is illegal, although some compilers support it. template class X { friend T; }; Has this been legalized in C++11? (Sorry, didn't have time to read the draft myself, just hoping someone knows this)
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
8
votes
1 answer

Difference of C++17 and C++20 in template friend function with unary and binary operators

I have the following MWE in C++20 with clang++ -std=c++2a, in which I defined in-class unary - operator and friend-ed binary - operator: template class vec; template vec operator-(const vec&, const…
Jay Lee
  • 1,684
  • 1
  • 15
  • 27
8
votes
4 answers

recursive friend classes

Is there any way around this: class B; class C { public: C() { } private: int i; friend B::B(); }; class B { public: B() { } private: int i; friend C::C(); }; Gives error: prog.cpp:8: error: invalid use of incomplete type…
BCS
  • 75,627
  • 68
  • 187
  • 294
8
votes
1 answer

g++ issues incomprehensible warning

system.h: #include namespace ss { class system { private: // ... public: // ... friend std::ostream& operator<< (std::ostream& out, const system& sys); }; } system.cpp: #include "system.h" std::ostream&…
Danish
  • 407
  • 3
  • 10
8
votes
3 answers

Friend functions of a class template

I have a class template Foo. I'd like to implement a non-member function Bar that takes two Foos and returns a Foo. I want Bar to be a non-member because it will be more natural for callers to write Bar(f1, f2) than f1.Bar(f2). I also want Bar…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
8
votes
1 answer

Regarding friend function definition and namespace scopes

I was reading this blog post section, and I tried to play around with the snippet that was provided. namespace N { // 2 class A { friend void f(A) {} // 1 }; } If I understood correctly, the definition in // 1 will inject the name f where // 2 is…
Dante
  • 404
  • 2
  • 10
8
votes
3 answers

Grant access to private constructor without friends?

I am working on some code, where I encountered a situation similar to this one: struct Bar; struct Foo{ friend struct Bar; private: Foo(){} void f(){} void g(){} }; struct Bar { Foo* f; Bar() { f = new Foo();} ~Bar() {…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185