Function Parameter That Accepts Regular Function And Member Function

Hello. I have a class which takes a function in its constructor. I need it to be able to take a member function of a different class and also regular functions. It compiles with the regular function but not the member function. How would I get it work for both types?

I have tried to condense my issue below. Thanks.

typedef void (*voidbyte) (byte);

class myClass
{
	public:
		voidbyte m_functionOne;
		myClass (voidbyte functionOne)
		{
			m_functionOne = functionOne;
		}
		myClass (voidbyte functionOne, voidbyte functionTwo)
		{
			//Do something else
		}
};

void regularFunction(byte input)
	{/*Do things*/}
void regularFunctionTwo(byte input)
	{/*Do things*/}

class MyOtherClass
	{
	public: 
		void memberFunction(byte unused){/*Do something*/}
	};

MyOtherClass thing;

static myClass classArray[3] = 
	{
	{(voidbyte)regularFunction},
	{(voidbyte)regularFunctionTwo},
	{(voidbyte)thing.memberFunction}//This is the bit that I cannot make work.
	};

void setup(){}
void loop()
	{
	classArray[0].m_functionOne(9);
	}

You can only do that if the member function being passed is static. c++ does not allow taking the address of non-static member functions, for reasons that become obvious once you understand how c++ functions are implemented.

Regards,
Ray L.

It will not work for both types. Member functions have a hidden extra parameter - a pointer to the instance.

Consider learning about C++ closures. It will do what you want, and they are quite the fashion these days.

C++ closures..

You understand this stuff? I saw what you posted and went to look it up. It sounded like string theory to me.

-jim lee

PaulMurrayCbr:
Consider learning about C++ closures. It will do what you want, and they are quite the fashion these days.

Thanks very much for the help, Paul. Closures were what I needed. I have updated the code below in case it helps anybody. It’s arbitrary and messy, but it shows it works, I think:

typedef void (*voidvoid) ();

class myClass
{
	public:
		voidvoid m_functionOne;
		voidvoid m_functionTwo;
		template<typename F>
		myClass (F func)
		{
			m_functionOne = func;
		}
		template<typename F, typename G>
		myClass (F func, G func2)
		{
			m_functionOne = func;
			m_functionTwo = func2;
		}
};

void regularFunction(byte thing)
	{
	Serial.write("regularFunction: ");
	Serial.print(thing);
	Serial.write("\n");
	}
void regularFunctionTwo(byte thing)
	{
	Serial.write("regularFunctionTwo: ");
	Serial.print(thing);
	Serial.write("\n");
	}

class MyOtherClass
	{
	public:
		bool isAlive = false;
		void memberFunction(byte thing){Serial.write(isAlive?"true":"false");Serial.write("\n");}
	};

MyOtherClass thing, otherThing;

static myClass classArray[3] = 
	{
	{[](){regularFunction(16)      ;}},
	{[](){thing.memberFunction(1)  ;},	[](){otherThing.memberFunction(4);}},
	{[](){regularFunctionTwo(64)   ;}}
	};

void setup(){
	Serial.begin(9600);
	thing.isAlive = true;
	delay(2000);
	}
void loop()
	{
	classArray[0].m_functionOne();
	classArray[1].m_functionOne();
	classArray[1].m_functionTwo();
	classArray[2].m_functionOne();
	delay(2000);
	}

Outputs:

regularFunction: 16
true
false
regularFunctionTwo: 64