Syntax for declaring a function that uses function pointers

Hey all,

I'm new to c++ and function pointers. I think I have all my function pointers defined correctly, but I can't find out how to declare a function (constructor, actually) that uses these pointers as arguments.

What is the syntax for this?

in example.h
1
2
3
4
5
6
7
8
9
10
...
class example
{
	private:
		(const char**) (funcPtr1);
		(const char**) (funcPtr2);
	public:
		example(const char**, const char**); //this is wrong
}
...


in example.cpp
1
2
3
4
5
example::example(const char** (*funcPtr1)(), const char** (*funcPtr2)())
{
	const char** arr1= (*funcPtr1)();
	const char** arr2= (*funcPtr2)();
}
closed account (zb0S216C)
Sythion wrote:
but I can't find out how to declare a function (constructor, actually) that uses these pointers as arguments. (sic)

Do you mean like this:

1
2
3
4
5
example( const char( **FunctionOne )( void ), const char( **FunctionTwo )( void ) )
{
    FunctionOne( );
    FunctionTwo( );
}


Wazzak
I would use

example::example(const char** (*funcPtr1)(), const char** (*funcPtr2)());

void parameters are not required for C++ code; they are supported to allow compilation of code by C as well as C++ compilers. I omit them from all C++ code.

Your class definition just needs to follow suit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class example
{
	private:
		const char** (*m_funcPtr1)();
		const char** (*m_funcPtr2)();
	public:
		example(const char** (*funcPtr1)(), const char** (*funcPtr2)());
};

example::example(const char** (*funcPtr1)(), const char** (*funcPtr2)())
{
	const char** arr1= (*funcPtr1)();
	const char** arr2= (*funcPtr2)();
	
	m_funcPtr1 = funcPtr1;
	m_funcPtr1 = funcPtr2;
}
Last edited on
PS A habit I have picked up from assorted people (authors, teams leads, ...) is to always use function pointers through typedefs. I find it makes things a lot clearer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class example
{
	private:
		typedef const char** (*PFNEXAMPLE)();

		PFNEXAMPLE m_funcPtr1;
		PFNEXAMPLE m_funcPtr2;
	public:
		example(PFNEXAMPLE funcPtr1, PFNEXAMPLE funcPtr2);
};

example::example(PFNEXAMPLE funcPtr1, PFNEXAMPLE funcPtr2)
{
	const char** arr1= (*funcPtr1)();
	const char** arr2= (*funcPtr2)();
	
	m_funcPtr1 = funcPtr1;
	m_funcPtr1 = funcPtr2;
}
Last edited on
Thanks!

I changed the code as follows, and it compiles (although I'm still a bit away from actually doing somehting useful with it).

in example.h
1
2
3
4
5
6
7
8
9
10
...
class example
{
	private:
		//(const char**) (funcPtr1);
		//(const char**) (funcPtr2);
	public:
		example(const char**(funcPtr1)(), const char**(funcPtr2)()); //this is wrong
}
...


in example.cpp
1
2
3
4
5
example::example(const char** (*funcPtr1)(), const char** (*funcPtr2)())
{
	const char** arr1= (*funcPtr1)();
	const char** arr2= (*funcPtr2)();
}


It's very odd that there is an identifier included in the declaration though. What's the purpose of that?
closed account (zb0S216C)
andywestken wrote:
void parameters are not required for C++ code

True, but some people prefer it as a preference since it explicitly indicates that the function doesn't require arguments. The problem I see with type-defined function pointers is that I have to go to their definition and see it that function pointer requires arguments, which is annoying.

Wazzak
Last edited on
I am aware that some people like to use void, but I don't get why "void" makes it clearer (or more explicit) that a function doesn't take a parameter as opposed to it not taking a parameter.

I find using typedefs makes the use of function pointer far less painful, esp. with more complicated functions. In general, the typedef is close to the use. When it isn't, I use a comment. Part of the reason I don't share your concern is that I use function pointers very sparingly, and then only ever through an excapsulating class. So once the class is in use, you don't go near the function pointer again.

Topic archived. No new replies allowed.