2013-06-22 23 views
1

假設我有一個C++非託管類,看起來像這樣C++/CLI公開內部成員管理類只

#include "note.h" 
class chord 
{ 
private: 
    note* _root; // or, as in my real class: std::shared_ptr<note> _root; 
    // _third, _fifth, _seventh, etc 
public: 
    someClass(const note n1, const note n2, const note n3); // constructor takes some of these notes to make a chord 

    std::shared_ptr<note> root() const; // returns ptr to root of the chord 
    std::string name() const; // returns the name of this chord 

} 
現在

,我知道我需要這兩個類包裝成CLI管理類。但問題是,我如何將私有指針傳遞給構造函數的本地類?

就目前而言,Note * _src在noteWrapper中是私有的。但本地Chord()需要本地Note對象。所以chordWrapper無法訪問noteWrappers _src,傳遞給構造函數。我怎樣才能做到這一點,而不暴露內部成員.net?

編輯**

// assume noteWrapper is already defined, with Note* _src as private 
public ref class chordWrapper 
{ 
    private: 
    Chord* _src; 
    public: 
    chordWrapper(noteWrapper^ n1, noteWrapper^ n2, noteWrapper^ n3) 
    { 
      _src = new Chord(*n1->_src, *n2->_src, *n2->_src); // _src is inaccessible 
    } 
} 

上述是不可能的,因爲chordWrapper具有與內部構件沒有訪問。由於朋友也不被支持,我不知道我能做些什麼來隱藏.net的內部成員,並將他們暴露給cli類。

處理這個問題的適當方法是什麼?

+0

通過創建一個引用類創建包裝該與本地班有相同的成員。所以你的chordWrapper也應該有一個構造函數,它需要三個noteWrapper參數。而且你應該有一個類型爲noteWrapper的* root *字段,使得getter變得微不足道。 –

+0

這裏的問題在於指向類(note,chord)的指針是私有的,並且僅在cli類中用於內部使用。然而cliChord類需要以某種方式訪問​​cliNotes私人指針,傳遞給本地和絃構造函數。這可能沒有將指針設置爲public? – Igneous01

+0

C++中的標準也適用於C++/CLI,請使用* friend *關鍵字。 –

回答

3

內部成員與C++/CLI中的私有成員在同一範圍內。它與C#內部修飾符相同。恕我直言,我認爲沒有可見性修飾符的類/結構將被解釋爲內部默認?

public ref class noteWrapper 
{ 
    Note* _src; 
} 

是在相同的範圍等

public ref class noteWrapper 
{ 
private: 
    Note* _src; 
} 

public ref class noteWrapper 
{ 
internal: 
    Note* _src; 
} 

是一個私有成員與CLI庫另外共享

3

您可以使用'internal'關鍵字僅與cli庫共享,而不是.Net客戶端。 例如

public ref class noteWrapper 
{ 
internal: 
    Note* _src; 
}