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:
(constchar**) (funcPtr1);
(constchar**) (funcPtr2);
public:
example(constchar**, constchar**); //this is wrong
}
...
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.
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.
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.
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.