C ++包装器将指针地址传递给本机类

时间:2015-06-30 10:04:13

标签: visual-studio c++-cli clr managed mixed-mode

您好我正在尝试将指针地址从托管类传递给非托管类 类。所以每个A-Object都有一个B对象的引用。但是如果我在_a = new A(_managedB->_b)中传递引用,编译器会抛出一个错误,没有构造函数匹配参数列表。

那么代码有什么问题?

非托管代码:

class A
{
private:
    B &b;
public:
    explicit
        A(B& b);
        ~A();
}

托管代码:

public ref class ManagedA
{
private:
    ManagedB ^_managedB;
    A *_a;
public:
    ManagedA::ManagedA(ManagedB ^managedB ): _managedB(managedB)
    {
        _a = new A(_managedB->_b);
    }
    ManagedA::~ManagedA(ManagedB ^managedB ): _managedB(managedB)
    {
        delete _a;
        _a = 0;
    }
};

1 个答案:

答案 0 :(得分:1)

Ok I did it like this and it worked. I don't know if the situation is interpreted right, but I am not trying to pass a Handle as a pointer. I pass a pointer that is a member of an Handle.

public ref class ManagedA
{
private:
    ManagedB ^_managedB;
    A *_a;
public:
    ManagedA::ManagedA(ManagedB ^managedB ): _managedB(managedB)
    {
        _a = new A(*_managedB->_b);
    }
    ManagedA::~ManagedA(ManagedB ^managedB ): _managedB(managedB)
    {
        delete _a;
        _a = 0;
    }
};