|I have two sets of classes, C++/CLI classes and native classes.
They are supposed to be counterparts of each other, used for passing messages & data between a managed module and a native module, with a mixed-mode module in between.
The CLI classes are set up thus: (You may have to meditate on this for a moment to see what I'm trying to do.)
public ref class Managed_base abstract
unsigned int _Data = 0;
virtual Native_base* GetNativeObject(unsigned int data) = 0;
public ref class Managed_derived : Managed_base
virtual Native_derived* GetNativeObject(unsigned int data) override
Native_derived* NativeObject = new Native_derived();
NativeObject->_Data = data;
}I'm trying to set it up so that if I call GetNativeObject on any managed derived class, it will return the corresponding derived native class.
That way I don't have to do any ugly casting when I call GetNativeObject.
But the compile error I get is,
"Virtual function overriding with a covariant return type is not allowed in a managed class."
So my question is, is there any way to accomplish what I'm trying to do without having to cast the return type of the GetNativeObject method to the appropriate derived type?
Is there any pattern that does this?
I eventually decided to do away with the pure virtual method and the overriding methods in derived classes. I simply put a regular function into each derived class that returns the appropriate derived native class. It is not beautiful, but it works.
However, I would like to leave this question active, in case someone has any interesting ideas.
The difficult we do right away...
...the impossible takes slightly longer.
modified 12-Sep-20 16:49pm.