开发者

What causes MOVE_CAST_ERROR when down-casting a reference object?

开发者 https://www.devze.com 2022-12-09 09:49 出处:网络
I\'m creating a class that inherits from a parent class with protected instantiation. The super class has a static method that provides an instance of the class.

I'm creating a class that inherits from a parent class with protected instantiation. The super class has a static method that provides an instance of the class.

The following code causes a run-time error MOVE_CAST_ERROR:

data: o_child type ref to zchild.

o_child ?= zparent=>provide_instance( ).

I have also tried:

data: o_parent type ref to zparent,
      o_child  type ref to zchild.

o_parent = zparent=>provide_instance( ).
o_child  ?= o_parent.

I have succesfully down-casted with other object types开发者_如何学C in the past - does anyone have an idea what to look for in ZPARENT or ZCHILD that may make the two classes incompatible?

Currently ZCHILD only has one extra method, which if added to the super class would break the abstraction of the class, so I'm not that keen to do it.


I didn't do any object oriented coding in abap. Nor much abap programming. But I think that this is a typical oo scenario. So here is my guess.

You can't cast an object to what it ain't.
You have created an instance of the parent class. Now the child class has the property of "myCandy" that the parent class don't have. So your object don't have this property. And then you cast it to child. What the poor run time has to do when you ask for ( or change ) the "myCandy" property? It can do nothing. So it disallow this cast.

The cast is only possible if the object was instantiated as the child and then it was casted to the parent object and then back again to the child object. The child has everything the parent has so there is no problem with this path.

0

精彩评论

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