What does "class" before a object method declaration mean?

Hi! When I check the declaration of TObject in "System" unit, I found these code:
[code]
TObject = class
constructor Create;
procedure Free;
class function InitInstance(Instance: Pointer): TObject;
[/code]

There is a keyword "class" before the declaration of function "InitInstance", can you tell me what does it mean or which part of help file shall I read?

Thank you!

Comments

  • Hi! I find the documentation of Class methods. It tells that "class" before the declaration means the method is a Class Method. The way to find this documentation is to type in "class method" from index of help.

    Documentation tells:
    "A class method can be called through a class reference or an object reference. When it is called through and object reference, the class of the object becomes the value of Self."

    However, I still don't know what is the difference between a Class Method and other methods.

    Also, "the class of the object becomes the value of Self" => "class becomes value of Self", here "Self" is a variable. I don't understand why class can become a variable.

    Thank you!
  • [b][red]This message was edited by injektilo at 2003-7-17 9:44:22[/red][/b][hr]
    this is the core of delphi, the class method is build in into the compiler itselve, not available for us.

    when calling it, it runs some code to register the class for windows wheter it is an object class or other, delphi knows what to do with it.

    so TObject is the first class object created with the classmethod and you should start from there, or if you are programming a compiler then you should start to learn ASM :)) (wich you probebly allready have)


  • Thank you! You told me something in the kernal of Delphi, which is very useful to understand a programming language.

    However, the thing currently I'm concerning is:
    what is the difference between "InitInstance" and other method defined without "class" in front of it.
    [code]
    TObject = class
    constructor Create;
    procedure Free;
    class function InitInstance(Instance: Pointer): TObject;
    [/code]

    Thank you!

  • : [b][red]This message was edited by injektilo at 2003-7-17 9:44:22[/red][/b][hr]
    : this is the core of delphi, the class method is build in into the compiler itselve, not available for us.
    :
    : when calling it, it runs some code to register the class for windows wheter it is an object class or other, delphi knows what to do with it.
    :
    : so TObject is the first class object created with the classmethod and you should start from there, or if you are programming a compiler then you should start to learn ASM :)) (wich you probebly allready have)
    :
    :
    :
    A class method is not something for the core of Delphi, but a method, which can also be applied to the class definition itself. Example:
    [code]
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    Button1.Caption := TButton.ClassName; // a class function
    end;
    [/code]
    This code will show the name of the class itself, not an instance of the object. Since ClassName() is a class method (see help file), this is a valid code, while the following code will generate an error:
    [code]
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    TButton.Click; // not a class method
    end;
    [/code]
    The Click() method cannot be called from the class definition of a TButton, but only from an TButton instance.
  • Thank you! I understand now.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories