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
• Tim in Simple TLS/SSL SMTP client for Qt5

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data OK

Tim in Simple TLS/SSL SMTP client for Qt5

simple-tls-ssl-smtp-client-for-qt Linux: UBUNTU 22.04 QT Creator 8.0.2 QT 6.3.2 I get get following: /home/timl/Development/smtp/smtp.cpp:152: error: no matching function for call...

Tim in Simple TLS/SSL SMTP client for Qt5

simple-tls-ssl-smtp-client-for-qt Linux: UBUNTU 22.04 QT Creator 8.0.2 QT 6.3.2 I get get following: /home/timl/Development/smtp/smtp.cpp:152: error: no matching function for call...

Tim in Simple TLS/SSL SMTP client for Qt5

simple-tls-ssl-smtp-client-for-qt Linux: UBUNTU 22.04 QT Creator 8.0.2 QT 6.3.2 I get get following: /home/timl/Development/smtp/smtp.cpp:152: error: no matching function for call...

Tim in Simple TLS/SSL SMTP client for Qt5

simple-tls-ssl-smtp-client-for-qt Linux: UBUNTU 22.04 QT Creator 8.0.2 QT 6.3.2 I get teg following: /home/timl/Development/smtp/smtp.cpp:152: error: no matching function for call...

Tim in Simple TLS/SSL SMTP client for Qt5

simple-tls-ssl-smtp-client-for-qt Linux: UBUNTU 22.04 QT Creator 8.0.2 QT 6.3.2 I get teg following: /home/timl/Development/smtp/smtp.cpp:152: error: no matching function for call...

seo in nRF51 Makefile with Qt Creator

thanksssssssssssssssssssss gfhfgjg