Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to define a virtual static member on a delphi class?
    primarykey
    data
    text
    <p>I need to create something similar which i implemented in PHP language. </p> <p>Assume that i created a base class which define 2 static member variable, and then the subclass should be able to 'override' them so if i have defined the static variable "someStatic" in the BaseClass, and then i subclass to DerivedClass, when i call TDerivedOne.someStatic, the program should be showing someStatic from that derived class.. but that's not the case with Delphi, i definitely implemented it wrong..</p> <p>For the time being, i implemented another design approach, in which the variables are not declared static, and then created a virtual abstract method called 'setup', and then this setup will be called on BaseClass constructor, but this design requires the object created first before we could retrieve the variable needed.</p> <p>Now out of curiosity, i wonder if the virtual static variables could be implemented to save 'a couple of typing..'</p> <p>Here is the code snippet of the problem</p> <pre><code>program Project1; {$APPTYPE CONSOLE} uses SysUtils; type TBaseClass = class protected class var someStatic: string; public class function get: string; virtual; end; TDerived = class( TBaseClass ) (* //if i uncomment this section, then the print bellow will print the Base's static var protected class var someStatic: string; *) end; TDerived2 = class( TBaseClass ) end; class function TBaseClass.get: string; begin Result := someStatic; end; begin // ------If the class is defined per unit, this one should be on the initialization section TBaseClass.someStatic := 'base'; TDerived.someStatic := 'derived'; TDerived2.someStatic := 'derived2'; // ------END OF INITIALIZATION try //i'm expecting this would print 'derived' but it prints 'derived2' :'( //i am using DelphiXE //apparently delphi treat the statics differently from that of php's writeln( TDerived.get ); readln; except on E: Exception do writeln( E.ClassName, ': ', E.Message ); end; end. </code></pre> <p>Cheers :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload