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

Make a static property shared between all instances of a class

S__MPay
Beginner
471 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
471 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

0 Kudos
4 Replies
Arjen_Markus
Honored Contributor I
471 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.

0 Kudos
S__MPay
Beginner
471 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)

0 Kudos
Arjen_Markus
Honored Contributor I
471 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.

0 Kudos
JAlexiou
New Contributor I
472 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`

0 Kudos
Reply