2016-05-30 4 views
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が含まれるヘッダで定義されています。

これを実現する方法はありますか?

+0

ですから、 'XScuGic'の2つのインスタンスを持っています。 1つのグローバル変数と1つは 'Foo'のプライベートメンバーですか?コピーをプライベートメンバーに入れたいですか?あなたはプライベートリファレンスメンバーを持つことができます。 – wally

+0

あなたのクラスに 'refference'や' pointer'を保存しなかったのはなぜですか? –

+0

@flatmouseはい、明示的に渡すことなく、同じ割り込みインスタンスを自分のクラスで使用できるようにします – Etruscian

答えて

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; 
} 
関連する問題