gpt4 book ai didi

C++ 将指针强制转换为静态方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:43 25 4
gpt4 key购买 nike

我好久没写C++代码了;但是现在我必须在 texas instruments F28335 DSP 上工作,并且我正在尝试从 C 迁移到 C++。我有以下代码试图用类的静态方法初始化中断服务例程:

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

编译时我得到以下信息:

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

我想我错过了一些类型转换。

最佳答案

我认为最根本的问题是 & 符号在你赋值给 p 的 RHS 前加上前缀。此外,“PINT”在其他操作系统中是“指向整数的指针”。因此,让我们避免任何潜在的名称冲突。但我认为这对你有用:

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

关于C++ 将指针强制转换为静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7428568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com