开发者

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);
/开发者_StackOverflow社区/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.


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;
}


I think you have several unrelated syntax mistakes here: as I understand your Epwm1InterruptHandler is supposed to return a pointer to a function of type interrupt, then first remove void from line

static interrupt void Epwm1InterruptHandler(void);

and

static interrupt void Epwm1InterruptHandler(void);

then make p of type interrupt, and then put brackets like this:

interrupt p;
p = &(EPWMManager::Epwm1InterruptHandler());


Aren't you missing a ; after typedef interrupt void (*PINT)(void)? Your code compiles for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜