Mixed types are not supported

送分小仙女□ 提交于 2019-12-06 13:16:01

It isn't actually a problem in this specific case, at least from what's visible, but the C++/CLI compiler tries to prevent you from shooting your leg off, missile style. The garbage collector moves objects when it compacts the heap. Which makes native objects very dangerous, any pointers to them will become invalid and will destroy the GC heap when you write through them. The collector is powerless to update those pointers, it cannot find them. The risk is too high so the compiler just forbids it.

An alternative is to declare these members as pointers instead and allocate the array with operator new in the class constructor.

private:
    unsigned char* UP;
    // etc..
public:
    MissileLauncher() {
        UP = new unsigned char[10];
        // etc..
    }
    ~MissileLauncher() {
        this->!MissileLauncher();
        UP = nullptr;   // Destructor may be called more than once 
    }
    !MissileLauncher() {
        delete[] UP;
        // etc...
    }

Note the requirement for a destructor and a finalizer to release the memory for these arrays. Defining a destructor also brings along the burden of the client programmer having to call it (Dispose() or using in a C# client program, delete or stack semantics in a C++/CLI program), skipping it for such a small allocation is not unreasonable. And last but not least, consider the sane solution and use a managed array:

private:
    array<Byte>^ UP;
    // etc..
public:
    MissileLauncher() {
        UP = gcnew array<Byte>(10);
        // etc..
    }

The problem is you're mixing managed and unmanaged types which is what the compiler warning means. The class is a managed class but the integer arrays count as unmanaged objects. This causes problems with garbage collection. Read all about it here:

http://blogs.msdn.com/b/branbray/archive/2005/07/20/441099.aspx

Why not use managed arrays?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!