So far I've understand the page, I could define for every function address a function pointer.
1 2
int (*ptr_1) (int) = inc;
void (*ptr_2)(int&) = inc;
But i have several addresses to overloaded fuctions to pass as arguments to other functions. Is there no way, passing the addresses of overloaded functions directly, without having define for everything a pointer?
@gentlegui
I think that reinterpret_cast is much better then (....)(my_var). Its name is ugly and this emphases it out to the reader. In my example I need the cast, otherwise cout evaluates the addresses into a bool.
#include <iostream>
int inc(int i) {
std::cout << "Called int inc(int i): i = " << i << std::endl;
return ++i;
}
void inc(int& i) {
std::cout << "Called void inc(int& i): i = " << i << std::endl;
++i;
}
//Takes a pointer to a function having a return type of void
//and taking a reference to an integer as an argument
void take_ptr( void(*ptr)(int&), int &arg_to_send )
{
ptr(arg_to_send);
}
//Takes a pointer to a function having a return type of int
//and taking an integer as an argument
void take_other_ptr( int(*ptr)(int) )
{
ptr(5);
}
int main()
{
int i = 5;
//Just pass the name of the function
take_ptr(inc, i);
take_other_ptr(inc);
}
4.13 Boolean Conversions
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
any other value is converted to true
I don't believe there is any general solution to this issue. Note that the two definitions in the OP are almost completely ambiguous and, as such, are useless as overloaded functions.
This would not compile:
1 2 3 4 5 6 7 8 9 10
#include <iostream>
int inc(int i) { return ++i; }
void inc(int& i) { ++i; }
int main()
{
int j = 0;
inc(j);
}
If you wish to be able to differentiate between functions, name them differently.
@cire
cire wrote:
"If you wish to be able to differentiate between functions, name them differently."
I need this for the 'connect' function from the Qt libraries. Here i have to pass pointers to overloaded methods which belongs to the library interface. So i can't name them differently.
I need this for the 'connect' function from the Qt libraries. Here i have to pass pointers to overloaded methods which belongs to the library interface. So i can't name them differently.
Use a cast to differentiate. That won't help with the code in the OP though, as the functions are ambiguous.
Actually, it will work with the code in the OP, although the line above: inc(i); would need to be explicitly differentiated with a cast for the compiler.
This is entirely the same as the previous "solution" you mentioned. The pointer is just unnamed.
And of course, you have undefined behavior here as well. You seem to enjoy that sort of thing. X is not an appropriate format specifier for a pointer, and there should be an explicit cast to void*.
Seems like a good moment to address:
gentleguy wrote:
Why don't you use printf?
Because it's not type safe and prone to error, as you've illustrated.