- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
program testtype baseinteger iend type basetype, extends(base) :: Ainteger jend type Atype, extends(base) :: Breal kend type Bclass(base), allocatable :: mallocate(A::m)m%j = 1write(*,*) m%jstopend program test
... error #6460: This is not a field name that is defined in the encompassing structure.m%j-------^
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi DONG Li,
I think I understand what you are trying to achieve.
However, if you want to "work" with the various mesh types in some generic form,you canoften can come up with someinterface common to all mesh types. E.g. this could be type-bound procedures (TBP), let's say for example you use a common procedure called Update(...).
You could then define the procedure Update(...) to be a TBP in your base mesh class and overload it in all mesh type extensions with the appropriate method for mesh1D, mesh2D, etc.
That way you can use a variable mesh that is of class(mesh) and work with it, even if it's a mesh1D, mesh2D, etc.. Within the TBP procedures, the generic variable mesh will be accessible as the specific type, e.g. in an overloaded Update(...) routine in mesh2d the passed type will be mesh2d, even ifis wascalled via the generic variable mesh by calling mesh%Update().
If it's not possible to come up withsuch acommon interface foryour extended types, than you have to use the SELECT TYPE construct. While I agree that it is quite cumbersome in the syntax, it has the advantage over e.g. C++ type casts, that it is alwayssafe, as it does not allow you to access a field/TBP that is not present in your instance of the class.
I am also trying to make significant use of the new OO features in our codes, and so far I managed to keep the SELECT TYPEs at a minimum by defining common interfaces via TBP. However, I want to warn you that the TBP facilities in IVF11 Update 5 still have some severe issues that can lead to internal compile errors or wrong executable code in more complex situations.
regards,
Thomas
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What it boils down to is that you need to define your fields/type-bound procedures at the highest level of the hierarchy that you intend to use them.
In your code,what would you expect to happen when you call:
allocate(base:m)
m%j = 1
That j field does not exist in the base class at all. You implicitly relied on the fact that you knew that you reserved m to be of type A. If that's the case, why not make m a class(A) object? This is still polymorphic isthere are further extensions to A.E.g. if you modify your code to something like this:
[fortran]program testRegards, Thomas
type base
integer i
end type base
type, extends(base) :: A
integer j
end type A
type, extends(A) :: B
real k
end type B
class(A), allocatable :: m
allocate(B::m)
m%j = 1
write(*,*) m%j
stop
end program test
[/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi DONG Li,
I think I understand what you are trying to achieve.
However, if you want to "work" with the various mesh types in some generic form,you canoften can come up with someinterface common to all mesh types. E.g. this could be type-bound procedures (TBP), let's say for example you use a common procedure called Update(...).
You could then define the procedure Update(...) to be a TBP in your base mesh class and overload it in all mesh type extensions with the appropriate method for mesh1D, mesh2D, etc.
That way you can use a variable mesh that is of class(mesh) and work with it, even if it's a mesh1D, mesh2D, etc.. Within the TBP procedures, the generic variable mesh will be accessible as the specific type, e.g. in an overloaded Update(...) routine in mesh2d the passed type will be mesh2d, even ifis wascalled via the generic variable mesh by calling mesh%Update().
If it's not possible to come up withsuch acommon interface foryour extended types, than you have to use the SELECT TYPE construct. While I agree that it is quite cumbersome in the syntax, it has the advantage over e.g. C++ type casts, that it is alwayssafe, as it does not allow you to access a field/TBP that is not present in your instance of the class.
I am also trying to make significant use of the new OO features in our codes, and so far I managed to keep the SELECT TYPEs at a minimum by defining common interfaces via TBP. However, I want to warn you that the TBP facilities in IVF11 Update 5 still have some severe issues that can lead to internal compile errors or wrong executable code in more complex situations.
regards,
Thomas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page