friend-function Questions

15

We make a non-member function a friend of a class when we want it to access that class's private members. This gives it the same access rights as a static member function would have. Both alternati...
Othelia asked 22/2, 2010 at 23:46

1

I want to define a function template of a class template. The code looks like this. template<int M> struct test{ private: int value; template<int N = 2 * M> friend auto foo(test co...

2

The following code, where the nested class Info designates two member functions of the outer class Impl as friends, compiles nicely with Visual C++ and g++, with the code as given below. But, if t...
Bogor asked 24/4, 2016 at 8:50

1

Solved

Consider the following: namespace N { struct A { }; struct B { B() { } B(A const&) { } friend void f(B const& ) { } }; } int main() { f(N::B{}); // ok f(N::A{}); // error } In ...

1

Solved

I want to declare std::make_unique function as a friend of my class. The reason is that I want to declare my constructor protected and provide an alternative method of creating the object using uni...
Pia asked 24/11, 2015 at 22:24

3

Solved

I created a class and I want to force anyone who's trying to construct an object, to use unique_ptr. To do that I thought of declaring the constructor protected and use a friend function that retur...
Carolyn asked 24/11, 2015 at 19:18

2

Solved

Consider the following code: #include <vector> template<typename T> class Container; template<typename T> Container<Container<T>> make_double_container(const std::ve...
Biebel asked 1/11, 2015 at 16:7

1

We all have used friend function both global level as well as class level in C++. I tried to search accross the internet how internally friend function has been implemented. What manipulation don...
Caelum asked 14/4, 2015 at 10:43

2

Solved

Why code below well compiled in g++ but get error on clang? #include <iostream> class Object {}; class Print { public: template <typename CharT> inline friend std::basic_ostream&lt...
Naman asked 17/1, 2015 at 19:42

1

The code template <typename T> void foo(const T& t) {} template <typename T> class A { template <> friend void foo<T>(const T& t) {} }; gives compile error ...

2

I have a class which has a friend function declared and defined inside the class and I'm calling this function from another function within the class. Clang compiler (3.3) complains for undeclared ...
Langue asked 6/7, 2014 at 20:29

4

Solved

I have the following code: struct M { friend void f() {} M() { f(); // error: 'f' was not declared in this scope } }; int main() { M m; } Live example Both g++4.8 and clang3.4 fail to com...
Drus asked 8/5, 2014 at 11:42

1

Solved

How to declare a variadic template function as a friend? For example as follows: template<class T> class A { friend ??? MakeA ??? ; // What should be placed here ??? A(T) {} }; templat...

6

Solved

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++...
Hon asked 26/8, 2013 at 17:42

9

Solved

Is it possible? class sample { private: int x; public: friend void fun(); }; friend function with no argument! In my opinion not possible Because friend functions are not "member" of class...
Fagaceous asked 30/7, 2013 at 8:22

3

Solved

In the book The C++ Programming Language, by Bjarne Stroustrup, the author introduces a class Matrix which has to implement a function inv(). In section 11.5.1, he talks about two possibilities of...
Beabeach asked 10/10, 2012 at 5:11

3

Solved

i'm trying to create a new thread with a class "CameraManager" but i have the following error: cannot convert '*void(CameraManager:: * )(void*) to void*( * )(void*) in pthread_create function ...
Audiovisual asked 17/8, 2012 at 12:44

2

Solved

I have a static library written in C++. I have also got the header files for the classes defined in the static library. Can I access the private members of the classes defined in the static librar...
Lyon asked 28/3, 2012 at 8:26

4

Solved

From http://www.learncpp.com/cpp-tutorial/142-function-template-instances/ class Cents { private: int m_nCents; public: Cents(int nCents) : m_nCents(nCents) { } friend bool operator>(Cen...
Tertius asked 28/2, 2012 at 5:42

5

Solved

Consider this code: namespace foo {} class A { class B { }; friend int foo::bar( B& ); }; namespace foo { int bar( A::B& ) { } } G++ 4.4.3 tells me: friendfun-innerclass.cpp...
Furiya asked 11/10, 2011 at 15:20

2

Solved

I read that an overloaded operator declared as member function is asymmetric because it can have only one parameter and the other parameter passed automatically is the this pointer. So no sta...

3

Solved

#include <iostream> class B; class A{ int a; public: friend void B::frndA(); }; class B{ int b; public: void frndA(); }; void B::frndA(){ A obj; std::cout << "A.a = " <<...
Backhouse asked 6/7, 2010 at 5:30

1

Solved

I was struggling with the issue described in this question (declaring a template function as a friend of a template class), and I believe the 2nd answer is what I want to do (forward declare the te...
Ross asked 24/11, 2009 at 0:57

© 2022 - 2024 — McMap. All rights reserved.