我正在尝试将main
中声明的变量放入我的类的私有变量中,而不将其作为构造函数的参数传递。我需要将中断控制器链接到多个硬件中断,而无需重新初始化中断实例,从而覆盖它。
XScuGic InterruptInstance;
int main()
{
// Initialize interrupt by looking up config and initializing with that config
ConfigPtr = XScuGic_LookupConfig(INTERRUPT_DEVICE_ID);
XScuGic_CfgInitialize(&InterruptInstance, ConfigPtr, ConfigPtr->BaseAddr);
Foo foo;
foo.initializeSpi(deviceID,slaveMask);
return 0;
}
和Foo类的实现:
class Foo
{
// This should be linked to the one in the main
XScuGic InterruptInstance;
public:
// In here, the interrupt is linked to the SPI device
void initializeSpi(uint16_t deviceID, uint32_t slaveMask);
};
deviceID和slaveMask在包含的标头中定义。
有没有办法实现这个目标?
答案 0 :(得分:0)
您可以使用使用全局变量的构造函数初始化私有类引用成员,因此无需在构造函数中传递它:
XScuGic InterruptInstance_g; // global variable
class Foo {
private:
const XScuGic& InterruptInstance; // This should be linked to the one in the main
public:
Foo() : InterruptInstance{InterruptInstance_g} {}; // private variable is initialized from global variable
void initializeSpi(uint16_t deviceID,uint32_t slaveMask); // In here, the interrupt is linked to the SPI device
};
int main()
{
// Initialize interrupt by looking up config and initializing with that config
ConfigPtr = XScuGic_LookupConfig(INTERRUPT_DEVICE_ID);
XScuGic_CfgInitialize(&InterruptInstance,ConfigPtr,ConfigPtr->BaseAddr);
Foo foo{}; // reference is not required as it will come from the global variable to initialize the private reference member
foo.initializeSpi(deviceID,slaveMask);
return 0;
}