## Passing methods as function parameters C++

This is more like a quick reference for myself, how to pass a method as a function argument. It turned out to be very useful for one of programming tasks. I had a lot of  "get" type methods with the same return value, and needed to construct a table of results. And passing method as argument, was a very neat solution.

First define a new type like this:

typedef int (blockItem::*NODE_METHOD) ();

nt is the methods return type. Change it accordingly.
blockItem is the class name which methods you want to access.

I tried to come up with a way where return type doesn't matter, only thing I could come up with was using QVariant, but it involves changing a lot of methods and is Qt specific. I was too lazy to do that. If anybody who is reading this, knows a way how to do it, then feel free to enlighten me and other readers by leaving a comment.

Then just make a function with one of the arguments the new type. Just look at the example how to use the method's pointer.

void EpicClass::callAnotherMethod(NODE_METHOD method, blockItem* myObject)
{
cout << "Magic: " << (myObject->*method)() << endl;
}

The calling would be something like this:

void EpicClass::writeEpicData()
{
for(blockItem* object : allObjects)
{
callAnotherMethod( &blockItem::getStock, object );
callAnotherMethod( &blockItem::getThroughput, object );
}
}

That's basically all it is to it. I can't seem to remember the syntax when it come to using the pointer. It's not like someone often passes method as a function argument.

• #### Implementing pulse oximeter using MAX30100

Mar 8, 2017 | by Raivis Strogonovs
• #### nRF51 Makefile with Qt Creator

Jun 4, 2016 | by Raivis Strogonovs
• #### USART, FreeRTOS and C++ on nRF51

Dec 14, 2015 | by Raivis Strogonovs
• #### Starting with nRF51 BLE and Qt Creator

Dec 12, 2015 | by Raivis Strogonovs
• #### Touch gesture recognition using body capacitance

Nov 29, 2014 | by Raivis Strogonovs
• #### Introduction to data encryption

Oct 4, 2014 | by Raivis Strogonovs
• #### MEMS (Part 2) – Guide to using gyroscope L3G4200D

Jun 17, 2014 | by Raivis Strogonovs
• #### Johannes Adendorff in Simple TLS/SSL SMTP client for Qt5

DId you get BCC or CC that working?

#### Johannes Adendorff in Simple TLS/SSL SMTP client for Qt5

DId you get BCC or CC that working?

#### Sagie Meshulam in MEMS (Part 1) - Guide to using accelerometer ADXL345

Do you know if I can use flexiplot and Visual Code using C++? if so, how

#### Sagie Meshulam in MEMS (Part 1) - Guide to using accelerometer ADXL345

Do you know if I can use flexiplot and Visual Code using C++? if so, how

#### Drint in Pigaso - SVG to gcode coverter

Please say where did you refer to create your own gcode generator,I could not find any paper or journal

#### Drint in Pigaso - SVG to gcode coverter

Please say where did you refer to create your own gcode generator,I could not find any paper or journal

#### Drint in Pigaso - SVG to gcode coverter

Please say where did you refer to create your own gcode generator,I could not find any paper or journal