简体   繁体   中英

c++/cli expose internal members to managed classes only

Assuming I have a c++ unmanaged class that looks like this

#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

}

now, I know that I will need to wrap both of these classes into managed classes in cli. But the question is, how do I pass the private pointer to the native class to the constructor?

As it stands, the Note* _src is private inside noteWrapper. but native Chord() requires the native Note object. so chordWrapper is unable to access noteWrappers _src, to pass to constructor. How can I accomplish this, without exposing internal members to .net?

EDIT**

// 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
     }
}

The above is not possible because chordWrapper has no access to that internal member. Since friend is also not supported, I do not know what else I can do to hide the internal members from .net, and expose them to the cli classes.

what is the appropriate way to handle this?

internal members are in same scope then private members in C++/CLI. it´s the same as the C# internal modifier. IMHO i think that classes/structs without visibility modifier will be interpreted as internal by default?

public ref class noteWrapper
{
    Note* _src;
}

is in the same scope like

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

but

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

is a private member shared additionally with the cli library

You can use the 'internal' keyword to only share that with the cli library, not .Net clients. eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM