Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ cast pointer to static method

I haven't write C++ code in a long while; however now I have to work on a texas instruments F28335 DSP and I am trying to migrate from C to C++. I have the following code that is trying to initialize an interrupt service routine with a static method of a class:

//type definition for the interrupt service routine
typedef interrupt void (*PINT)(void);
//EPWMManager.h
class EPWMManager
{
public:
    EPWMManager();      
    static interrupt void Epwm1InterruptHandler(void);  
};
//EPWMManager.cpp
interrupt void EPWMManager::Epwm1InterruptHandler(void)
{
 //some code to be called on interruption
}   
//main.cpp
int main(void)
{
    PINT p;
    p = &(EPWMManager::Epwm1InterruptHandler);
    return 0;
 }

When compiling I get the following:

error: a value of type "void (*)()" cannot be assigned to an entity of type "PINT"

I guess I'm missing some cast.

like image 568
TropE Avatar asked Nov 04 '22 14:11

TropE


1 Answers

I think the fundamental problem is that ampersand prefixing the RHS of your assignment to p. Also, "PINT" is "pointer to integer" in other operating systems. So let's avoid any potential name conflicts. But I thinks this will work for you:

// you may have to move "interrupt" keyword to the left of the "void" declaration.  Or just remove it.
typedef void (interrupt *FN_INTERRUPT_HANDLER)(void);

interrupt void EPWMManager::Epwm1InterruptHandler(void)
{
 //some code to be called on interruption
}  

int main(void)
{
    FN_INTERRUPT_HANDLER p;
    p = EPWMManager::Epwm1InterruptHandler; // no ampersand

    // and if for whatever reason you wanted to invoke your function, you could just do this:

   p(); // this will invoke your function.

    return 0;
}
like image 97
selbie Avatar answered Nov 12 '22 19:11

selbie