Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Casting operator ?

Former Member
0 Likes
629

Hello,

Im reading the documentation on =? operator, and dont really understand how it works, can any body tell me its functionality?

Thanks

Rajesh Thomas.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
490

This is used in ABAP objects. It is uses when moving an object refernce from one to another. For example. Lets say tht you have an object vehicle and and object car. Car is a vehicle, so the casting would be successful.

vehicle ?= car.

If it was reverse the casting would fail, because vehicle is not car.

car ?= vehicle.

Regards,

Rich Heilman

PS.

Put yourself on the SDN world map (http://sdn.idizaai.be/sdn_world/sdn_world.html) and earn 25 points.

Spread the wor(l)d!

2 REPLIES 2
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
491

This is used in ABAP objects. It is uses when moving an object refernce from one to another. For example. Lets say tht you have an object vehicle and and object car. Car is a vehicle, so the casting would be successful.

vehicle ?= car.

If it was reverse the casting would fail, because vehicle is not car.

car ?= vehicle.

Regards,

Rich Heilman

PS.

Put yourself on the SDN world map (http://sdn.idizaai.be/sdn_world/sdn_world.html) and earn 25 points.

Spread the wor(l)d!

Read only

0 Likes
490

This operator is used when ur widening the cast. i.e., assigning a reference of a superclass to a subclass.

for eg:

u have 3 classes airplane, cargo_airplane1, cargo_airplane2; airplane being the superclass and the other 2 being its subclasses.

create object cargo_airplane1.

airplane = cargo_airplane1.

cargo_airplane2 ?= airplane.

The type of case described above is known as a widening cast because it changes the view to one with more details. The instance assigned (a cargo plane in the above example) must correspond to the object reference (cargo_airplane1 in the above example), that is, the instance must have the details implied by the reference

The widening cast in this case does not cause an error because the reference airplane actually points to an instance in the subclass lcl_cargo_airplane. The dynamic type is therefore ‘REF TO lcl_cargo_airplane’.

But if u try creating an object of airplane and assign it to cargo_airplane1 directly -

cargo_airplane ?= airplane

this would result in runtime error.

Here the widening cast produces the MOVE_CAST_ERROR runtime error that can be caught with “CATCH ... ENDCATCH”, because the airplane reference does not point to an instance in the subclass lcl_cargo_airplane, but to a “general airplane object”. Its dynamic type is therefore ‘REF TO lcl_airplane’ and does not correspond to the reference type cargo_airplane.

Hope it is clear now.

-Aarthi.