In fortran 2003, classes and OOP are defined in the standard. I would like to know how upcasting and downcasting is performed.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Actually you can do up-casting (but not down-casting) out-of-the-box using this approach:
Note:
Or you can define an assignment from child type to parent:
In that case you do not need to use polymorphic entities and special form of ALLOCATABLE statement:
Down-casting… Hmmm… It’s unsafe, it’s against strong typing discipline. When I faced with down-casting I tsarted to think in the same way – using the same approach. You need to just define another assignment – from parent to child. The only problem will be that if you will use exactly the same scheme (GENERIC binding) child_from_parent will be not distinguishable from parent_from_child. However you can do it in another way:
But this is not a down-casting. Down-casting is casting a reference to the base class to one of its derived classes. You need to check whether the type of the referenced object is indeed the one being cast to or a derived type of it, and thus issue an error if it is not the case.
Friday night… Good time to do some Fortran. =) Finally I ended up with: