问题描述:

The idea is that I would like to reuse code and not duplicate it. Below, UnManagedB derives from UnmanagedA. Both the structs have their corresponding managed wrappers as shown below. I would like to derive the ManagedB from ManagedA so that I can reuse the properties in ManagedA for ManagedB. The issue is both the class have their own pointers to unmanaged objects. The unmanaged object in the derived is all I want and I want all the properties to use UnManagedB pointer. Is there any way to do this?

struct UnManagedA {

unsigned int size;

};

struct UnManagedB:UnManagedA {

int length;

int width;

};

public ref class A : public System::IDisposable {

public:

A();

!A();

~A();

property System::UInt32 Size {

System::UInt32 get();

void set(System::UInt32 value);

}

internal:

UnmanagedA* GetUnmanaged() { return obj1; }

private:

UnmanagedA* obj1;

};

public ref class B : public System::IDisposable, public A {

public:

B();

!B();

~B();

property System::UInt32 Length {

System::UInt32 get();

void set(System::UInt32 value);

}

property System::UInt32 Width {

System::UInt32 get();

void set(System::UInt32 value);

}

internal:

UnmanagedB* GetUnmanaged() { return obj2; }

private:

UnmanagedB* obj2;

};

网友答案:

While they both have their own pointer to unmanaged objects, presumably those pointers point to the exact same unamanged object--so there should be no problem. If you disagree describe a specific usage scenario where it wouldn't work correctly, and then we can better help.

相关阅读:
Top