C ++ / CLI:返回对非托管对象的引用

时间:2015-03-13 04:46:46

标签: c# c++ c++-cli

我正在寻找一种方法来将引用返回给另一个作为托管对象成员的对象。这可以在C ++中轻松实现,但对于与C#一起使用的C ++ / CLI包装器来说是一个挑战。下面是可重复的场景(它是很多代码,但它很容易,只是展示了问题)

C ++类:

class NATIVEAPI NativeSlave
{
public:
    NativeSlave() : x_( 0 ), y_( 0.0 )
    {}

    NativeSlave( int x, double y ) : x_( x ), y_( y )
    {}

    int x_;
    double y_;
};

class NATIVEAPI NativeMaster
{
public:
    __declspec( property( get = getSlave, put = setSlave ) ) NativeSlave& slave;
    NativeSlave& getSlave()
    {
        return *pSlave; //returns a reference
    }
    void setSlave( const NativeSlave& slave )
    {
        *pSlave = slave;
    }

public:
    NativeMaster() : pSlave( new NativeSlave( 4, 5.0 ) )
    {}
    ~NativeMaster()
    {
        delete pSlave;
    }

private:
    NativeSlave* pSlave;
};

C ++用法:

NativeSlave slave = NativeSlave( 1, 2.0 ); //now, slave.x==1, slave.y==2.0
NativeMaster master; //now, master.slave.x==4, master.slave.y==5.0

master.slave = slave; //now, master.slave.x==1, master.slave.y==2.0
master.slave.x_ = 6; //now, master.slave.x==6
master.slave.y_ = 10.0; //now, master.slave.y==10.0

因此,在C ++中,我们可以轻松获得对底层对象的引用并对其方法进行操作(此处,成员公开以简化示例)。

然后,目标是将其包装在C ++ / CLI中,以在C#中实现与上述C ++相同的功能(用法):

C#(所需)

ManagedSlave slave = new ManagedSlave(1, 2.0); //now, slave.x==1, slave.y==2.0
ManagedMaster master = new ManagedMaster(); //desired: master.slave.x==4, master.slave.y==5.0

master.slave = slave; //desired: master.slave.x==1, master.slave.y==2.0
master.slave.x = 6; //no appropriate get() method to change master.slave
master.slave.y = 10.0; //no appropriate get() method to change master.slave

这是尝试编写包装器:

C ++ / CLI(在get / set属性方法中存在问题):

public ref class ManagedSlave
{
public:
    property int x
    {
        int get()
        {
            return mSlave->x_;
        }
        void set( int x )
        {
            mSlave->x_ = x;
        }
    }
    property double y
    {
        double get()
        {
            return mSlave->y_;
        }
        void set( double y )
        {
            mSlave->y_ = y;
        }
    }
public:
    ManagedSlave( int x, double y ) : mSlave( new NativeSlave( x, y ) )
    {}
    ~ManagedSlave()
    {
        delete mSlave;
    }

internal:
    NativeSlave* mSlave;
};

public ref class ManagedMaster
{
public:
    property ManagedSlave^ slave
    {
        ManagedSlave^ get()
        {
            //??????????????????????????
        };
        void set( ManagedSlave^ slave )
        {
            //is this correct???????????
            mMaster->slave.x_ = slave->x;
            mMaster->slave.y_ = slave->y;
        };
    }

public:
    ManagedMaster() : mMaster( new NativeMaster() )
    {}
    ~ManagedMaster()
    {
        delete mMaster;
    }

internal:
    NativeMaster* mMaster;
};

1 个答案:

答案 0 :(得分:1)

.NET"参考"与C ++引用完全不同。

.NET中有一些东西相当于C ++引用,ref关键字限定了参数。但除了参数之外,没有办法将它用于任何事情。 (在IL级别,它也可以用于局部变量,但仍然不能用于返回类型)

大多数情况下,您可以通过额外的间接层解决此问题。

在您的特定情况下,它非常简单:

ManagedSlave( NativeSlave* s ) : mSlave( s )
{}
~ManagedSlave()
{
    // empty
}

ManagedSlave^ ManagedMaster::slave::get()
{
     return gcnew ManagedSlave( &mMaster->getSlave() );
}

// remove ManagedMaster::slave::set

基本上,ManagedSlave没有理由负责分配和释放NativeSlave,因为NativeMaster已经这样做了。