I'm wondering what is the proper way to point at an overloaded function. Is it correct to cast the function to the proper type?

#include <iostream>

int double_it(int x)
{
	return x * 2;
}

int double_it(int x, int y)
{
	return (x + y) * 2;
}


int main(int argc, char**argv)
{
	void *myfunc = (void*)(int(*)(int))double_it;
	return 0;
}
mike_2000_17 commented: nice question +2

Recommended Answers

All 3 Replies

Member Avatar for nileshgr

I had just forgot about function pointers :D
Curious, I tried this and it worked.

#include <iostream>
using namespace std;

int fn(int a)
{
  cout << "Integer function called";
  return 0;
}

int fn(char a)
{
  cout << "Character function called";
  return 0;
}

int main()
{
  int (*fintptr)(int) = &fn;
  int (*fcharptr)(char) = &fn;
  fintptr(1);
  cout << "\n";
  fcharptr('a');
  cout << "\n";
}

So the code posted below is proper.

#include <iostream>

typedef int(*pfunc1)(int);
typedef int(*pfunc2)(int, int);

int double_it(int x)
{
	return x * 2;
}

int double_it(int x, int y)
{
	return (x + y) * 2;
}


int main(int argc, char**argv)
{
	pfunc1 func_1 = double_it;
	pfunc2 func_2 = double_it;

	std::cout << func_1(123) << std::endl;
	std::cout << func_2(345, 567) << std::endl;
	return 0;
}
Member Avatar for nileshgr

Seems to be. If the compiler doesn't complain, its fine :)
That's my way of doing things :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.