Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
2 Views

Make a static property shared between all instances of a class

Jump to solution

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

Accepted Solutions
Highlighted
New Contributor I
2 Views

Quote:Arjen Markus wrote:

Jump to solution

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`

John Alexiou

View solution in original post

0 Kudos
4 Replies
Highlighted
Valued Contributor II
2 Views

You can define a parameter in

Jump to solution

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
Highlighted
Beginner
2 Views

Quote:Arjen Markus wrote:

Jump to solution

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
Highlighted
Valued Contributor II
2 Views

No, if you do it this way,

Jump to solution

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
Highlighted
New Contributor I
3 Views

Quote:Arjen Markus wrote:

Jump to solution

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`

John Alexiou

View solution in original post

0 Kudos