Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

  • earth_walkerearth_walker Posts: 184Member
    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!
  • injektiloinjektilo Posts: 113Member
    [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)


  • earth_walkerearth_walker Posts: 184Member
    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!

  • zibadianzibadian Posts: 6,349Member
    : [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.
  • earth_walkerearth_walker Posts: 184Member
    Thank you! I understand now.

Sign In or Register to comment.