Forum > General
Class (static) fields of class. Initial values.
FTurtle:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- TSomeClass = class private class var a: Pointer; public b: Pointer; end;
Situation for regular fields (b) is known.
I interesting on class (static) fields (a).
In fact after program is run, TSomeClass.a=nil
But I can't find it in FPC documentation.
So, the question:
Can I rely on the fact that after starting program TSomeClass.a=nil
molly:
According to class variables documentation:
--- Quote ---Similar to objects, a class can contain static fields or class variables: these fields or variables are global to the class, and act like global variables, but are known only as part of the class. They can be referenced from within the classes’ methods, but can also be referenced from outside the class by providing the fully qualified name.
--- End quote ---
Variable documentation states:
--- Quote ---By default, simple variables in Pascal are not initialized after their declaration. Any assumption that they contain 0 or any other default value is erroneous: They can contain rubbish. To remedy this, the concept of initialized variables exists. The difference with normal variables is that their declaration includes an initial value, as can be seen in the diagram in the previous section.
--- End quote ---
and
--- Quote ---Managed types are an exception to this rule: Managed types are always initialized: in general this means setting the reference count to zero, or setting the pointer value of the type to Nil. See section 3.9, page 215
--- End quote ---
Answer from documentation: it depends.
FTurtle:
I read what you have quoted. It was the cause of question.
I'm cautious about phrases like "acts like" and their extrapolation.
I prefer explicit mentions.
I interesting simple types, so the answer is still "No".
But may be there is something what I don't know.
taazz:
Correct. Even if the behavior is documented it is possible that the next version of the compiler will alter the initialization. It is far better to make sure that your static variables are initialized in what ever value you desire by setting it on the initialization section of the unit they are defined. I'm assuming that you do try to keep the static fields to a minimum.
Thaddy:
Well let me spell it out: So yes, the class var is nil. And that is as per the documentation.
Navigation
[0] Message Index
[#] Next page