Lifetime of protected varaible

According to System Verilog IEEE 1800-2012 Standards
“class scope resolution operator enables Access to public or protected class members of a superclass from within the derived classes”.

Does the above statement mean that the Life time of a protected variable is static.

Can anyone Please clarify.

In reply to ram_33:
No. The class scope resolution operator is just a way of accessing class members; it has no effect on their declaration. You are probably confused by the way you normally access static class variables from outside the class. In that case, you have to use the scope operator. But from within a derived class, you can refer to class member up the inheritance hierarchy using the scope operator.

class A;
  int i; // defines A::i
  virtual function void print; // defines A::print
    $display("A::i",,i);
  endfunction
endclass
class B extends A;
  int i; // defines B::i
  virtual function void print; // defines B::print
    $display("B::i",,i);
  endfunction
endclass
class C extends B;
  int i; // defines C::i
  virtual function void print; // defines C::print
    $display("C::i",,i);
  endfunction
  function void printall;
    $display("A::i",,A::i);
    $display("B::i",,B::i); // B::i same as super.i
    $display((C::i",,C::i); // can't do super.super.i
    print(); // calls C::print() if there are no overrides of print() in extended classes
    C::print()// always calls C::print();
  endfunction
endclass

In reply to dave_59:

Thanks Dave.

In reply to dave_59:

I thought that “super” is used only to access shadowed tasks/functions of the extended class.

Does it mean that the constructor of C is going to create A and B instances, when instantiating C? And we are interested in values of “i” in A and B.

In this case what are the values of A::i and B::i from the example above?
I think that they are supposed to be the same as C::i.

Alex

In reply to Alex K.:

“super” is used to access what you would have inherited had you not defined the same identifier in the current class.

The constructor of C creates one instance(object), an instance of a C type. That instance includes all the members inherited from B and A class types. So A::i, B::i, and C::i all exist as separate variables in a C instance.

I didn’t assign any values to the variables in my example, but I could have given each one of them a different value.

Please note it is usually a poor programming practice to give names to variables in an extended class that already exist in the base class type. Normally this name override is done with class methods only.

In reply to dave_59:

Does it mean that inside the definition of class B, A::my_task() will print “A” and B::my_task() will print “B”?

class A;
int i = 1;
task my_task();
$display(“A\n”);
endtask
endclass

class B extends A;
int i = 2;
task my_task();
$display(“B\n”);
endtask
task try();
A::my_task(); // will it print “A”?
B::my_task(); // will it print “B”?
endtask
endclass

And what about the external use?

B b_inst = new();
b_inst.my_task(); // “B” will be printed?
$display(b_inst.i); // “2” will be printed?

Thanks,
Alex

In reply to Alex K.:

I think you should try running the code and see what happens.

In reply to dave_59:

Thank you, Dave.