Class Property Lifetime

Where in the LRM does it state that class properties have automatic lifetime by default? Section 6.21 contains:

Class methods (see Clause 8) and declared for loop variables (see 12.7.1) are by default automatic, regardless of the lifetime attribute of the scope in which they are declared.

Is this a typo where “methods” should be “members”?

Section 6.21 Scope and lifetime describes lifetime as a property of variables. It also mentions that tasks and functions [(methods)] may be declared as automatic. While it doesn’t use the term lifetime when describing methods, the BNF in 9.2 Structured procedures shows lifetime being used in the function_declaration production. The statement that class members have automatic lifetime also appears in 8.6 Object methods.

Coming back to your question, what do you mean by class properties have automatic lifetime by default? Are you sure you’re not confusing dynamic/static (belongs to object/class) with lifetime (exists for the whole simulation)? Check out 8.10 Static methods.

Hi Tudor. I am talking about the difference between


class a;
   int b;
endclass

class c;
   static int d;
endclass

where each ‘a’ object has its own ‘b’ property, while property ‘d’ is shared amongst all objects ‘c’. I believe that this is considered lifetime, and that the lifetime of ‘b’ is automatic, however the LRM specifies it in 8.9 as:

The previous examples have only declared instance class properties. Each instance of the class (i.e., each object of type Packet) has its own copy of each of its eight variables.

which is not very explicit. I filed an issue on it.

Interestingly, the LRM consistently uses the term “non-static members” rather than “automatic members”. So it looks like methods are either automatic or static, while properties are non-static or static.

In reply to sbellock:

The lifetime of non-static class properties are dynamic. Variables declared inside a class methods are not class members. Automatic lifetimes only apply to non-static variables declared inside procedural scopes.

In reply to sbellock:

This is static vs. dynamic, which is not the same as the concept of lifetime.

In reply to Tudor Timi:

Right. So there are two, maybe three, characteristics at play here: the lifetime of the class property and the uniqueness of the class property.


class a;

    int b;          // (Lifetime) Class property with (non-static / dynamic) lifetime.
                    // The lifetime of the property is the lifetime of the class instance / object.
                    // (Uniqueness) Each class instance / object contains its own copy of the property.

    static int c;   // (Lifetime) Class property with static lifetime.
                    // The lifetime of the class property is the entirety of simulation.
                    // (Uniqueness) Only one copy of class property exists.
                    // This single class property exists even if no objects are constructed.
endclass

Please let me know if I have that wrong.

It appears that the LRM defines the uniqueness characteristics of both (non-static / dynamic) and static class properties in section 8.9:

The previous examples have only declared instance class properties. Each instance of the class (i.e., each object of type Packet) has its own copy of each of its eight variables. Sometimes only one version of a variable is required to be shared by all instances. These class properties are created using the keyword static.

I contend, and please correct me if I’m wrong, that the LRM does not specify the lifetime of (non-static / dynamic) class properties, or even name them as (non-static / dynamic) class properties in either 6.21 or all of section 8. This leads to people incorrectly interpreting the LRM and assuming (as I did) that if it’s not static then it must be automatic. See here and here:

It’s worth noting that the LRM does explicitly define the lifetime of class methods in 8.6:

The lifetime of methods declared as part of a class type shall be automatic.

It would be good to have something similar in 8.5, specifying at least:

The default lifetime of properties declared as part of a class type shall be dynamic.