Application Development 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: 

? operator

Former Member
0 Kudos

go_employee ?= cl_pt_employee=>get_employee( p_pernr ).

What does this statement mean. In normal abap there is no ? operator.

Pls advice

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi,

The ?= operator implements a widening cast, also called a down cast.

Whenever a static type check is not possible or when the type checks are to be performed at program runtime, you must use the statement

 MOVE ... ?TO ... 

or the casting operator (?=). The casting assignment replaces the assignment operator (=). In the MOVE... ? TO statement, or when you use the casting assignment, there is no static type check. Instead, the system checks at runtime whether the object reference in the source variable points to an object to which the object reference in the target variable can also point. If the assignment is possible, the system makes it, otherwise, the catchable runtime error MOVE_CAST_ERROR occurs.

Syntax rules force you to use casting whenever a static type check is not possible, for example:

 cref1 ?= iref1. 

Here, an interface reference is assigned to a class reference. For the casting to be successful, the object to which iref1 points must be an object of the same class as the class of the class variable cref1 or one of its subclasses..

Hope this helps.

Thanks,

Balaji

5 REPLIES 5

former_member188770
Active Participant
0 Kudos

Hi,

This operator is used in ABAP OOP concept.

It is used for type casting .

Thanks,

Poonam.

0 Kudos

can u explain more on this?

0 Kudos

Hi ,

Please consider a example for widening cast

You use widening cast to move the view to more details ex. 'Cargo airplane'.

Data : Airplane type lcl_airplane,

cargo_airplane type lcl_cargoairplane,

cargo_airplane2 type lcl_cargoairplane

Create object cargo_airplane.

airplane = cargo_airplane . (Narrowing cast)

cargo_airplane2 ?= airplane. (widening cast)

Hope this helps.

Thanks,

Poonam.

Former Member
0 Kudos

Hi,

The ?= operator implements a widening cast, also called a down cast.

Whenever a static type check is not possible or when the type checks are to be performed at program runtime, you must use the statement

 MOVE ... ?TO ... 

or the casting operator (?=). The casting assignment replaces the assignment operator (=). In the MOVE... ? TO statement, or when you use the casting assignment, there is no static type check. Instead, the system checks at runtime whether the object reference in the source variable points to an object to which the object reference in the target variable can also point. If the assignment is possible, the system makes it, otherwise, the catchable runtime error MOVE_CAST_ERROR occurs.

Syntax rules force you to use casting whenever a static type check is not possible, for example:

 cref1 ?= iref1. 

Here, an interface reference is assigned to a class reference. For the casting to be successful, the object to which iref1 points must be an object of the same class as the class of the class variable cref1 or one of its subclasses..

Hope this helps.

Thanks,

Balaji

Former Member
0 Kudos

hai Abraham,

Narrowing cast means that the assignment changes from a more specialized view (with visibility to more components) to a more generalized view (with visibility to fewer components).

Narrowing cast is also referred to as “up cast” . “Up cast” means that the static type of the target variable can only change to higher nodes from the static type of the source variable in the inheritance tree, but not vice versa.

Reference variable of a class assigned to reference variable of class : object

 Class c1 definition.
Endclass.

Class c1 implementation.
Endclass.
Start-of-selection.
Data:
orefc type ref to object,
oref1 type ref to c1.

Create object :oref1.

.

Narrowing Cast : Static type of target more general than Static type of source.

Orefc = oref1.

Regards.

Eshwar.