Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
27429 Discussions

Make a static property shared between all instances of a class

S__MPay
Beginner
255 Views

Is it possible to define a static property that is shared between all instances of a class?

The case is a element class which uses a vector class.

I want that all element objects have a vector_i and vector_j which define the global normal vectors (i.e vector_i = vector(1, 0, 0) and vector_j = vector(0, 1, 0)).

0 Kudos
1 Solution
JAlexiou
New Contributor I
255 Views

Arjen Markus wrote:

You can define a parameter in the module that contains the definition:

module vectors

module vectors
    real, dimension(3), parameter :: vector_i = [1.0, 0.0, 0.0]

   ...
end module vectors

That parameter would be available to all objects you define via this module.

 

FYI, The same trick works for custom types also

 

    type(vector3), parameter :: &
        o_ = vector3(0.0,0.0,0.0), &
        i_ = vector3(1.0,0.0,0.0), &
        j_ = vector3(0.0,1.0,0.0), &
        k_ = vector3(0.0,0.0,1.0)

 

PS. I use a different convention of `i_` instead of `_i`

View solution in original post

4 Replies
Arjen_Markus
Honored Contributor I
255 Views

You can define a parameter in the module that contains the definition:

module vectors

module vectors
    real, dimension(3), parameter :: vector_i = [1.0, 0.0, 0.0]

   ...
end module vectors

That parameter would be available to all objects you define via this module.

S__MPay
Beginner
255 Views

Arjen Markus wrote:

You can define a parameter in the module that contains the definition:

module vectors

module vectors
    real, dimension(3), parameter :: vector_i = [1.0, 0.0, 0.0]

   ...
end module vectors

That parameter would be available to all objects you define via this module.

 

Markus, thank; but I was thinking if this may make an independent parameter for each instance and take more memory? (my concern is reduce memory usage)

Arjen_Markus
Honored Contributor I
255 Views

No, if you do it this way, the vectors will exist (may be an inadequate terminology) outside any object. Only when you put some component vector_i or vector_j in the class definition will they occupy memory in each one.

JAlexiou
New Contributor I
256 Views

Arjen Markus wrote:

You can define a parameter in the module that contains the definition:

module vectors

module vectors
    real, dimension(3), parameter :: vector_i = [1.0, 0.0, 0.0]

   ...
end module vectors

That parameter would be available to all objects you define via this module.

 

FYI, The same trick works for custom types also

 

    type(vector3), parameter :: &
        o_ = vector3(0.0,0.0,0.0), &
        i_ = vector3(1.0,0.0,0.0), &
        j_ = vector3(0.0,1.0,0.0), &
        k_ = vector3(0.0,0.0,1.0)

 

PS. I use a different convention of `i_` instead of `_i`

Reply