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

How do I access a private constructor in a separate class?

I'm writing a library in C++. I have two classes in my library, A and B. I want to hide the A() constructor from any code that references my library. I also want class B to be able to call the A() constructor. I come from a C# background and…
user2023861
  • 8,030
  • 9
  • 57
  • 86
11
votes
6 answers

C++ friend function hidden by class function?

Minimal example: class A { friend void swap(A& first, A& second) {} void swap(A& other) {} void call_swap(A& other) { swap(*this, other); } }; int main() { return 0; } g++ 4.7 says: friend.cpp: In member function ‘void…
Johannes
  • 2,901
  • 5
  • 30
  • 50
11
votes
2 answers

Template friendship

I'm trying to access protected variables of a template class with different template parameters. A friend declaration with template parameters is giving the following error: multiple template parameter lists are not allowed My code is template
Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
11
votes
4 answers

Implementation of Friend concept in Java

How does one implement the friend concept in Java (like C++)?
FirstName LastName
  • 1,891
  • 5
  • 23
  • 37
10
votes
3 answers

Declaring C++ static member functions as friends of the class in which it resides (syntax)

What is the syntax for declaring a static member function as a friend of the class in which it resides. class MyClass { private: static void Callback(void* thisptr); //Declare static member friend static void Callback(void* thisptr); //Define as…
unixman83
  • 9,421
  • 10
  • 68
  • 102
10
votes
4 answers

How do I make main a friend of my class from within a library?

Please see my first attempt at answering this . I neglected to tell the whole story before in an attempt to simplify things. Turns out my example works! Sorry. The whole story is that this is a library the contains a class in one file and the main…
Jaime
  • 1,182
  • 2
  • 12
  • 29
10
votes
1 answer

unconventional uses of friend in c++

I know the general use cases for the friend keyword with regards to encapsulation but one a couple of occasions, I have needed the friend keyword just to "get the job done". These use cases don't make me happy so I'm wondering if there are some…
10
votes
1 answer

Not able to befriend typedefs: any particular reason?

struct A {}; typedef A B; struct C { friend struct B; }; GCC 4.7.0 20110427 tells me error: using typedef-name 'B' after 'struct'. So far, this seems pretty self-explanatory; after all, my example code is trying to declare-and-friend a struct…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
10
votes
4 answers

public friend function in C++?

I saw some code in C++ and have a question about it: class CRectangle { int width, height; public: friend CRectangle duplicate (CRectangle); }; The variables width and height are private, and the method duplicate() is…
user707549
10
votes
2 answers

C++: Bad practice to use friend classes instead of writing getters / setters?

I have two classes which in one aspect work together tightly. They both use functionality of each other that should be only used by them and not by any other class. Is it bad practice if I make those two classes friends so they can directly access…
Jarx
  • 101
  • 1
  • 1
  • 3
10
votes
2 answers

An example of the use of a `friend` specifier in a destructor?

[class.dtor]/1 contains the following sentence: Each decl-specifier of the decl-specifier-seq of a destructor declaration (if any) shall be friend, inline, or virtual. I'm really interested in seeing an example of the use of a destructor with a…
Alexander
  • 2,581
  • 11
  • 17
10
votes
2 answers

Forward declaration of template friend function

Consider the following code snippet that works perfectly fine: class A { private: int d; public: A(int n){ d = n;} friend int foo(A a); }; int foo(A a) { return a.d; } However, when I try to use a template for the class, I need to…
Cantfindname
  • 2,008
  • 1
  • 17
  • 30
10
votes
3 answers

friend with class but can't access private members

Friend functions should be able to access a class private members right? So what have I done wrong here? I've included my .h file with the operator<< I intent to befriend with the class. #include using namespace std; class…
starcorn
  • 8,261
  • 23
  • 83
  • 124
10
votes
1 answer

Is a friend function template defined in the class available for lookup? clang++ and g++ disagree

Here is the code: struct foo { template friend foo f() { return {}; } }; int main() { auto x = f(); // clang++ can't find it, g++ can. } clang++ 3.4 gives: fni2.cpp:8:12: error: use of undeclared identifier 'f' auto x =…
kec
  • 2,099
  • 11
  • 17
10
votes
3 answers

There are some details I didn't understand in §7.3.1.2/3 in the C++11 Standard

§7.3.1.2/3 in the C++11 Standard (emphasis are mine): Every name first declared in a namespace is a member of that namespace. If a friend declaration in a nonlocal class first declares a class or function the friend class or function is a…
Wake up Brazil
  • 3,421
  • 12
  • 19