1

我试图将在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 在包含的标头中定义。

有没有办法做到这一点?

4

1 回答 1

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;
}
于 2016-05-30T13:45:02.630 回答