Pointer to friend function.

May 3, 2010 at 3:14pm
Hello

I wonder if it is possible to use pointers to friend functions. I wrote a code that tried to do it, but I wasn't able to compile it until I changed that function to regular function.

Does anyone have any experience with that?
May 3, 2010 at 4:47pm
is the friend function a global function or a member function of a class?
if it is global simply do this:

1
2
3
void *myfunctptr = myfunct; //provided it is not overloaded
//am not so sure if it is a member function of another class, you can try the following
void *myfunctptr = myclass::myfunct;
May 3, 2010 at 5:44pm
I must object.

It is a terrible idea to cast function pointers to void pointers.

awful awful awful.
May 3, 2010 at 5:47pm
To be more specific. Why this code doesn't want to compile?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

using namespace std;

class myclass {
	private:
		int a;
		int b;
	public:
		myclass(int i, int j) : a(i), b(j) {};
		friend int sum(const myclass & obj) {
			return obj.a + obj.b;
		}
		friend int difference(const myclass & obj) {
			return obj.a - obj.b;
		}
};

void calculate(int (*fun)(const myclass & obj));

int main() {
	calculate(sum);
}

void calculate(int (*fun)(const myclass & obj)) {
	myclass temp(3,7);

	cout << fun(temp) << endl;
}


I get this error:

/C++/main.cpp: In function ‘int main()’:
/C++/main.cpp:22: error: ‘sum’ was not declared in this scope
May 3, 2010 at 5:53pm
That function is missing a few arguments, that function is a member of a class that has no instance named sum, and... you know what, here's a link for you to read:
http://cplusplus.com/doc/tutorial/classes/

-Albatross
Last edited on May 3, 2010 at 5:54pm
May 3, 2010 at 6:00pm
I'm sorry, but I don't get it ...

Sum is friend function not member function and as far as I know you don't invoke it by using class object.
May 3, 2010 at 6:12pm
There seem to be a number of misunderstandings in the snippet posted. I suggest reading up on both friend functions and static methods to start.

As far as passing the member function as an argument, I would recommend against it altogether. It can be done but I suspect there are better solutions at your disposal.
May 3, 2010 at 6:24pm
I have not written this post without prior searching for solution in other materials. Maybe I'm missing something very basic, but I would really appreciate if someone could point that specifically ... not sending me to some other vague places ...

And once again sum is not a member function, it is friend function.
May 3, 2010 at 6:39pm
1
2
3
friend int sum(const myclass & obj) {
			return obj.a + obj.b;
		}


Doing this creates a member function called sum, and the friend is redundant since all member functions are friends.
May 3, 2010 at 6:44pm
http://www.cplusplus.com/doc/tutorial/inheritance/

Notice that you declare a friend function in a class but the function is defined outside of the class.
May 3, 2010 at 7:21pm
Thank You for responses. Indeed moving definitions out from class declaration solves compilation problem. But it is first time that I hear that friends have to be defined outside of class. Very interesting ...
May 3, 2010 at 8:32pm
OK, I looked at that a bit more and found out that neither of solutions given here so far is correct. The problem is a bit different. To show that you can simple divide the code I showed before into 3 files
* myclass.h with class declaration
* myclass.cpp with class methods and friend functions definitions
* main.cpp

In that configuration it will give the same error as the beginning.

The true problem is with friend function scope -- check Stroustrup - The C++ Programming Language 11.5.1. Basically friend functions are not in scope and solution is to put declarations of friend functions once again before main (lines 42, 43; difference doesn't second declaration because definition is in scope).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

class myclass {
	private:
		int a;
		int b;
	public:
		myclass(int i, int j) : a(i), b(j) {};
		friend int sum(const myclass & obj);
		friend int difference(const myclass & obj);
		friend int times(const myclass & obj) {
			return obj.a * obj.b;
		}
};

#endif // MYCLASS_H

myclass.cpp

#include "myclass.h"

int sum(const myclass & obj) {
	return obj.a + obj.b;
}

main.cpp

#include <iostream>
#include "myclass.h"

using namespace std;

int difference(const myclass & obj) {
	return obj.a - obj.b;
}

void calculate(int (*fun)(const myclass & obj));

int sum(const myclass & obj);
int times(const myclass & obj);

int main() {
	calculate(sum);
	calculate(difference);
	calculate(times);
}

void calculate(int (*fun)(const myclass & obj)) {
	myclass temp(3,7);
	cout << fun(temp) << endl;
}

May 4, 2010 at 1:38am
FWIW, a friend declaration is not a prototype.

Technically speaking, to be standard compliant, you must declare sum, difference, and times in
myclass.h after the class:

1
2
3
int sum( const myclass& );
int difference( const myclass& );
int times( const myclass& );

Topic archived. No new replies allowed.