Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29251 ディスカッション

Make a static property shared between all instances of a class

S__MPay
ビギナー
1,085件の閲覧回数

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 件の賞賛
1 解決策
JAlexiou
新規コントリビューター I
1,085件の閲覧回数

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`

元の投稿で解決策を見る

4 返答(返信)
Arjen_Markus
名誉コントリビューター II
1,085件の閲覧回数

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
ビギナー
1,085件の閲覧回数

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
名誉コントリビューター II
1,085件の閲覧回数

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
新規コントリビューター I
1,086件の閲覧回数

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`

返信