开发者

how to make managed wrapper class use the properties of another managed wrapper class?

开发者 https://www.devze.com 2023-02-27 06:33 出处:网络
The idea is that I would like to reuse code and not duplicate it. Below, UnManagedB derives from UnmanagedA. Both t开发者_StackOverflowhe structs have their corresponding managed wrappers as shown bel

The idea is that I would like to reuse code and not duplicate it. Below, UnManagedB derives from UnmanagedA. Both t开发者_StackOverflowhe 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消