- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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)).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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 vectorsThat 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`

Link Copied

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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 vectorsThat 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)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

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 vectorsThat 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`

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