C ++ calling a function through an object with an open member pointer for a function, without using the dereference operator

Well, I think the title is quite descriptive (still confusing, sorry).

I read this library: Timer1 .

The header file has an open member pointer to a function as follows:

class TimerOne { public: void (*isrCallback)(); }; 

There is an instance of a TimerOne object called "Timer1".

Timer1 calls the function as follows:

 Timer1.isrCallback(); 

How is that right? I am familiar with function calls using function pointers using the dereference operator.

Example:

 (*myFunc)(); 

So, I expected that the above call through the object would become something more similar:

 (*Timer1.isrCallback)(); 

So, what are the acceptable options for calling functions using function pointers, both stand-alone function pointers and object elements?

+4
source share
3 answers

Things you can do with a function pointer.

1: The first function call through explicit dereferencing:

 int myfunc(int n) { } int (*myfptr)(int) = myfunc; (*myfptr)(nValue); // call function myfunc(nValue) through myfptr. 

2: The second way is through implicit dereferencing:

 int myfunc(int n) { } int (*myfptr)(int) = myfunc; myfptr(nValue); // call function myfunc(nValue) through myfptr. 

As you can see, the implicit dereferencing method looks just like a regular function call - this is what you expect, because the function is simply implicitly converted to function pointers!

In your code:

 void foo() { cout << "hi" << endl; } class TimerOne { public: void(*isrCallback)(); }; int main() { TimerOne Timer1; Timer1.isrCallback = &foo; //Assigning the address //Timer1.isrCallback = foo; //We could use this statement as well, it simply proves function are simply implicitly convertible to function pointers. Just like arrays decay to pointer. Timer1.isrCallback(); //Implicit dereference (*Timer1.isrCallback)(); //Explicit dereference return 0; } 
+4
source

You do not need to look for a function pointer to call it. According to the standard ([expr.call] / 1),

A postfix expression must have a function type or a pointer to a function type.

So (*myFunc)() , and therefore myFunc() . Actually, (**myFunc)() also valid, and you can play as many times as you want (can you understand why?)

+5
source

You asked:

Timer1 calls the function as follows:

 Timer1.isrCallback(); 

How is that right?

Type Timer1.isrCallback - void (*)() . This is a pointer to a function. That is why you can use this syntax.

This is similar to using:

 void foo() { } void test_foo() { void (*fptr)() = foo; fptr(); } 

You can also use:

 void test_foo() { void (*fptr)() = foo; (*fptr)(); } 

but the first form is equally valid.

Update in response to OP comment

Given the published class definition that you would use:

 (*Timer1.isrCallback)(); 

To use

 (Timer1.*isrCallback)(); 

isrCallback must be defined as a non-member variable whose type is a pointer to a member variable in TimerOne .

 void (TimerOne::*isrCallback)(); 

Example:

 #include <iostream> class TimerOne { public: void foo() { std::cout << "In TimerOne::foo();\n"; } }; int main() { TimerOne Timer1; void (TimerOne::*isrCallback)() = &TimerOne::foo; (Timer1.*isrCallback)(); } 

Exit:

 In TimerOne::foo(); 

(Check this code)


If you want to define isrCallbak as a member variable in TimerOne , you will need to use:

 #include <iostream> class TimerOne { public: void (TimerOne::*isrCallback)(); void foo() { std::cout << "In TimerOne::foo();\n"; } }; int main() { TimerOne Timer1; Timer1.isrCallback = &TimerOne::foo; // A little complicated syntax. (Timer1.*(Timer1.isrCallback))(); } 

Output:

 In TimerOne::foo(); 

(Check this code)

+1
source

All Articles