At my workplace, we have code where the class function refers to Self , effectively creating a potential violation of access rights if the object is not initialized. Does the meaning of Self method of the class, how to refer to the class, and not to the object?
I assume that the only reason this works is because Self refers to a different class function , rather than a regular method.
I created the following test class:
constructor TTest.Create; begin FTest := 'Hej'; end; procedure TTest.GoTest; begin ShowMessage(FTest); end; class procedure TTest.Test; begin Self.GoTest; end;
However, when compiling, this causes an error:
[dcc32 Error] Unit1.pas(51): E2076 This form of method call only allowed for class methods or constructor
I also tested direct access to FTest, which gave another error:
[dcc32 Error] Unit1.pas(51): E2124 Instance member 'FTest' inaccessible here
But I wonder, however, whether it is possible to have a case where Self potentially dangerous in class methods.
I know that referring to global objects and I assume that they are always there.
source share