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

Best Models for a Friendship relation (in Django)

What is the best way to model friendships between users for a social networking site? The possible states are: no Friendship FriendRequest from A to B, B needs to confirm (this is asymmetric) A and B are friends (this is symmetric) now its…
allo
  • 3,955
  • 8
  • 40
  • 71
9
votes
1 answer

friend declaration of template specialization fails

The following code containing friend declaration fails with indicated error (see http://ideone.com/Kq5dy): template void foo() {} template class A { void foo(); friend void foo(); // error: variable or field 'foo'…
Gene Bushuyev
  • 5,512
  • 20
  • 19
9
votes
2 answers

How can I call a private destructor from a shared_ptr?

I have a resource_manager class which maintains a std::vector > internally. resource_manager is a friend class of resource. I want resources to only be created/deleted by resource_manager, so I made its constructors…
Dan Nestor
  • 2,441
  • 1
  • 24
  • 45
9
votes
3 answers

c++ implementing friend/inline functions

I can't seem to find the answer to this newbie question. If I have a class // Header file (.h) Class X { public: friend bool operator==(const X&, const X&); inline size_type rows() const; }; etc... when I go to implement the .cpp file of X,…
theAir
  • 91
  • 1
  • 2
9
votes
2 answers

What is the rationale behind the syntax chosen to declare template friends?

Declaring template function friends involves some incredibly unintuitive syntax, even for C++! What is the rationale behind the choice of syntax for the extra <> needed? Wouldn't it make more sense to use the template keyword? For those that don't…
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
9
votes
1 answer

A friend function accessing a private member of a friend class

Following the Czech song from Eurovision 2019 in Tel-Aviv It is known that in C++ a friend of a friend is not (automatically) a friend. Clang however differs on the following code with GCC and MSVC: class A { public: // forward declaration …
Amir Kirsh
  • 12,564
  • 41
  • 74
9
votes
3 answers

Correct friend definition to give std::map access to private default constructor

I am working on a library which uses a struct which should not have the default constructor accessible by users of the lib. struct Example { Example(int x); private: Example(); }; Inside the library the the default constructor is…
Beginner
  • 5,277
  • 6
  • 34
  • 71
9
votes
1 answer

g++ and clang++ different behaviour with friend template function defined inside a template class

Another question of type "who's right between g++ and clang++?" for C++ standard gurus. The following code template struct foo { template friend void bar () { } }; int main () { foo<0> f0; foo<1> f1; …
max66
  • 65,235
  • 10
  • 71
  • 111
9
votes
4 answers

warning defining friend operator declared inside a namespace

Someone can explain me a warning from g++ ? Given the following code #include namespace foo { struct bar { friend std::ostream & operator<< (std::ostream &, bar const &); }; } std::ostream & foo::operator<< (std::ostream & o,…
max66
  • 65,235
  • 10
  • 71
  • 111
9
votes
2 answers

partial template specialization for friend classes?

I have a simple class X, and set of templatized classes Y. I'd like all classes Y where the first templatization parameter happens to be X to be a friend of X itself. The following hopefully conveys what I want, but the friend statement gives…
Matthew Busche
  • 551
  • 4
  • 12
9
votes
1 answer

VS2015 and clang compile this code, but g++ rejects it. Which one is correct?

VS2015 and clang compile this code, but g++ rejects it. namespace A { struct B { friend void f(); }; } void A::f() {} int main(){ } I think g++ is right because of this Note in 7.3.1.2/3: If a friend declaration in a non-local class…
João Afonso
  • 1,934
  • 13
  • 19
9
votes
1 answer

Nonmember friend function is always inline

I am very new to C++, and when I am trying to learn the friend function, I saw from friend description on Cppreference that: 2) (only allowed in non-local class definitions) Defines a non-member function, and makes it a friend of this class at the…
Molly P
  • 131
  • 1
  • 4
9
votes
5 answers

what is the difference between friend function and friend class?

what is the difference between friend function and friend class? and where should be use of friend keyword?
ankit
  • 877
  • 3
  • 9
  • 14
9
votes
1 answer

Why does GCC not allow inheriting from a private nested class when you are a friend?

Same question is asked: Why does GCC allow inheriting from a private nested class? For non template classes, its allowed to inherit from private nested classes, if it is a friend, but not for template classes. Is it a bug? template class…
gerdi
  • 165
  • 6
9
votes
6 answers

How to make boost::make_shared a friend of my class

I have written a class with protected constructor, so that new instances can only be produced with a static create() function which returns shared_ptr's to my class. To provide efficient allocation I'd like to use boost::make_shared inside the…
kyku
  • 5,892
  • 4
  • 43
  • 51