Hi,

May I ask what is the correct syntax to achieve the following function pointer pattern?

I keep getting
error C2064: term does not evaluate to a function taking 0 arguments

Thank you.

class Apple
{
public:

	Apple(int i)
	{
		if(i == 1)
		{
			juicer = &Apple::one;
		}
		else if(i == 2)
		{
			juicer = &Apple::two;
		}
	}

	int (Apple::*juicer)();

	int one()
	{
		return 1;
	}

	int two()
	{
		return 2;
	}

	int internaljuicer()
	{
		this->juicer();
	}
};

class Basket
{
public:
	Basket(Apple* a)
	{
		apple = a;
	}

	Apple* apple;

	int getjuicer()
	{
		apple->juicer();
	}
};

int main()
{
	Apple apple(1);
	Basket basket(&apple);

	std::cout << basket.getjuicer() << std::endl;

	return 0;
}

The syntax for your call is not quite right. See below...

#include <iostream>

#define CALL_MEMBER_FN(object,ptrToMember)  ((object).*(ptrToMember)) 

class Apple
{
public:

   typedef int (Apple::*juicer_ptr)();

   Apple(int i)
   {
      if(i == 1)
      {
         juicer_ = &Apple::one;
      }
      else if(i == 2)
      {
         juicer_ = &Apple::two;
      }
   }

   juicer_ptr juicer_; 

   int one()
   {
      return 1;
   }

   int two()
   {
      return 2;
   }

   int internaljuicer()
   {
      return (this->*juicer_)();
   }
};

class Basket
{
public:
   Basket(Apple* a)
   {
      apple = a;
   }

   Apple* apple;

   int getjuicer()
   {
      return apple->internaljuicer();
   }
};
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.