托管和非托管类之间的继承

时间:2014-12-09 09:00:09

标签: .net pointers inheritance c++-cli mixed-mode

您好我在混合C ++和C#的项目中有关于继承的问题。所以我有一个C ++ / CLI层来处理中间的东西。 在C ++中,我有2个结构:

public Class A : 
{
public :
 A(){*a = new int(); *a = 0;}
 void f1(){*a = 10};
 int geta(){return *a};
private :
 int *a;
}

public Class B : public A
{
public :
 B(){*b = new int(); *b = 0;}
 void f2(){*b = 5; *a = 10};
 int getb(){return *b};
 int getinheriteda(){return *a;};
private : 
 int *b
}

然后在C ++ / CLI中,我的托管版本中有2个相同的类。每个人都拥有一个指向非托管C ++类的指针。

public ref Class ANet : 
{
public :
 ANet(){ un_a = new A();}
 ~ANet(){ this->!ANet();}
 !ANet(){ delete un_a;}
 void f1Net(){ un_a->f1();}
 int getanet(){return un_a->geta();}
private:
 A *un_a; //Pointer to the unmanaged A
}

版本1:

public ref Class BNet : public Class ANet:
{
public :
 BNet(){ un_b= new B();}
 ~BNet(){ this->!BNet();}
 !BNet(){ delete un_b;}
 void f2Net(){ ((B*)un_a)->f2();}
 int getbnet(){return un_b->getb();}
 int getinheriteda(){return un_b->getinheriteda();};
private:
 B *un_b; //Pointer to the unmanaged B
}

第2版:

public ref Class BNet : public Class ANet:
{
 BNet(){ un_a = new B();}
 ~BNet(){ this->!BNet();}
 !BNet(){ delete un_a;}
 void f2Net(){ ((B*)un_a)->f2();}
 int getbnet(){return((B*)un_a)->getb();}
 int getinheriteda(){return ((B*)un_a)->getinheriteda();};
private:
 //No pointer, use inherited un_a;
}

问题:

版本1:如果我得到B的实例,那么我有两个指针(un_b和继承的un_a),所以每个指针都得到了非托管类,导致不一致。

版本2:如果我得到B的实例,那么我有一个指针,但是创建了两次导致不一致

如何实现可以包装这2个非托管类的托管C ++ / CLI结构。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

C ++方式是在ANet中使用单独的构造函数来接受非托管指针:

public ref class ANet
{
...
protected:
    ANet(A* a) : un_a(a) { ... }
...

public ref class BNet : public ANet
{
public:
   BNet() : ANet(new B()) { ... }
...

在.Net中,您还可以在ANet构造函数中调用虚方法来创建非托管实例,并在需要时覆盖它:

public ref class ANet
{
public:
    ANet() { un_a = CreateUnmanagedInstance(); }
...
protected:
   virtual A* CreateUnmanagedInstance() { return new A(); }

public ref class BNet : public ANet
{
...
protected:
   virtual A* CreateUnmanagedInstance() override { return new B(); }
...

但是由于这种方法不适用于本机C ++类,因此可能会被认为太棘手且有害。