c++/cli 仅向托管类公开内部成员

Ign*_*s01 2 .net c# c++ wpf c++-cli

假设我有一个 c++ 非托管类,看起来像这样

#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

}
Run Code Online (Sandbox Code Playgroud)

现在,我知道我需要将这两个类都包装到 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
     }
}
Run Code Online (Sandbox Code Playgroud)

以上是不可能的,因为 chordWrapper 无法访问该内部成员。由于也不支持朋友,我不知道我还能做些什么来隐藏 .net 的内部成员,并将它们暴露给 cli 类。

处理这个问题的适当方法是什么?

Ale*_*ape 7

内部成员与 C++/CLI 中的私有成员在同一范围内。它与 C# 内部修饰符相同。恕我直言,我认为没有可见性修饰符的类/结构默认会被解释为内部?

public ref class noteWrapper
{
    Note* _src;
}
Run Code Online (Sandbox Code Playgroud)

在同一个范围内

public ref class noteWrapper
{
private:
    Note* _src;
}
Run Code Online (Sandbox Code Playgroud)

public ref class noteWrapper
{
internal:
    Note* _src;
}
Run Code Online (Sandbox Code Playgroud)

是与 cli 库额外共享的私有成员