Board index » delphi » Eqiv to C++ static class var?

Eqiv to C++ static class var?

What is the delphi equiviant to :

class something
{

private:

        static int someint;

Quote
}

or basically a variable that retains it's value irregardless of
instantiation. I know class methods exist in Delphi, is the same true of
variables?

Ryan Groth

 

Re:Eqiv to C++ static class var?


Quote
Ryan Groth wrote:

> What is the delphi equiviant to :

> class something
> {

> private:

>         static int someint;
> }

> or basically a variable that retains it's value irregardless of
> instantiation.

Technically speaking, there is no such beast.
However, if you declare your "Something" class in its own unit
and then put the line

var SomeInt: Integer;

in the implementation section, then you have the exact same
functionality.

Chris.

Re:Eqiv to C++ static class var?


Quote
Ryan Groth wrote:

> What is the delphi equiviant to :

> class something
> {

> private:

>         static int someint;
> }

> or basically a variable that retains it's value irregardless of
> instantiation. I know class methods exist in Delphi, is the same true of
> variables?

        There are no class fields or class properties - you could have a
property with a class read method. Not that I quite see the point, but
for example

type
     THmm = class
      private
        class function GetIt: integer;
      public
        property It:integer read GetIt;
     end;

class function THmm.GetIt:integer;
begin
   result:=42;
end;

procedure TForm1.Button1Click(Sender: TObject);
var h: THmm;
begin
   ShowMessage(inttostr(h.It));
end;

        Seems simpler just to have an ordinary field, set its value in the
constructor and then not let anything change it - or if you really need to
use uncreated objects you could just have a class function without the
property attached.

        Hmm, maybe I do see some point to having a field that you can
access through a class reference - don't think you can do that.

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:Eqiv to C++ static class var?


Quote
sna...@infi.net (Ryan Groth) wrote:
>What is the delphi equiviant to :
>class something
>{
>private:
>    static int someint;
>}
>or basically a variable that retains it's value irregardless of
>instantiation. I know class methods exist in Delphi, is the same true of
>variables?
>Ryan Groth

I don't know exactly what this C++ variable type is 'doing', but in
Delphi there is a "Typed Constant", and a codesnip can look like this:

function StaticCounter : integer;
const
   Cntr: integer = 0;
begin
   Inc(Cntr);
   Result := Cntr;
end;

Is it the 'thing', or....

---------------------------------
      o_kj...@post4.tele.dk
---------------------------------

Re:Eqiv to C++ static class var?


I'm currently studying Object Pascal, but it would seem that to carry
out the intent of static data members in Object Pascal, one could just
declare the 'someint' in the same unit that contains the class
'something.' The declaration of 'someint' would be in the
implementation part and then when code in the members of class
'something' refer to 'someint,' each object of the class will be
refering to the same variable. 'someint' would be initialized in the
initialization section of the unit.

In this case, 'someint' is hidden from users of the unit since its in
the implementation section, but its "global" to the objects of class
'something'.

This is essentially what the C++ code doing.

---------------------------------------------------
This message was created and sent using the {*word*104}dog Mail System
---------------------------------------------------

Re:Eqiv to C++ static class var?


Quote
>What is the delphi equiviant to :
>class something
>{
>private:
>    static int someint;
>}
>or basically a variable that retains it's value irregardless of
>instantiation. I know class methods exist in Delphi, is the same true of
>variables?

        You can declare a local module variable in the same unit and use it. You could then declare a
property which references that module level variable.

        There is probably a better solution, this is just from the top of my head.
--
Deploy your Delphi Apps ON the web. No more CGI/HTML. Just Delphi.
Does not require ActiveX or any Plugins.
Don't believe it? Come see BIOLIFE (The one that comes with Delphi) on the web.
http://www.shoresoft.com

Re:Eqiv to C++ static class var?


Quote
David Ullrich wrote:
> Ryan Groth wrote:
> > What is the delphi equiviant to :
> > class something
> > {
> >         static int someint;
> > }

> type
>      THmm = class
>       private
>         class function GetIt: integer;
>       public
>         property It:integer read GetIt;
>      end;

Not the same as C== static member tho' coz the value of
'It' is specific to each instance not to the class.

In Delphi you would need to declare a variable within the
implementation part of the same unit (to keep it "private")
which defines the class method:

unit THmm;
interface;
 type
      THmm = class
       private
         class function GetCount: integer;
         class function SetCount(i : integer): integer;
      end;

implementation;
var THmm_Count : integer; // hide the 'class' variable

function THmm.GetCount : integer;
begin
        result := THmm_Count;
end;

function THmm.SetCount(i : integer) : integer;
begin
        THmm_It := i;
        result := 0;
end;

constructor THmm.Create;
begin
   // ...
   inc(THmm_Count); { increment the global instance count }
end;

destructor Done;
begin  
   dec(THmm_Count); { one less instance }
end;

end.

--
----------------------------------------------------------------------------
A.J. Gauld                              Internet:  alan.ga...@gssec.bt.co.uk
BT, Glasgow Engineering Centre,         CompuServe:100341,442
24 Highburgh Rd,                        Phone:     +44 141 220 6250
Glasgow, G12 9ND, UK.                   FAX:       +44 141 220 6100
----------------------------------------------------------------------------

Re:Eqiv to C++ static class var?


Quote
David Ullrich wrote:
> [Some stuff that was probably missing the point entirely]

My C is somwehere between weak and non-existent. I said I didn't
see the point to something - of course there would be a point
to a constant that was stored with the class instead of with each
instance - for every instance to have a field with value 42 would
be stupid.
        And of course there would be a lot of excellent points to
having an assignable variable that belonged to the class instead
of the instance (for instance you could have a class field
InstanceExists that you could use to limit the class to one
instance.)

        Don't think you can do that (with something belonging to
the class, as opposed to a global variable in some unit - that
works, of course.) Although now that I see the point to it I'm
surprised you can't - maybe you actually can?

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Re:Eqiv to C++ static class var?


Ryan Groth <sna...@infi.net> wrote in article
<5e03h7$dp...@mh002.infi.net>...

Quote
> What is the delphi equiviant to :

> class something
> {

> private:

>    static int someint;
> }

> or basically a variable that retains it's value irregardless of
> instantiation. I know class methods exist in Delphi, is the same true of
> variables?

There are no class variables.  You can declare a variable in the
implementation section of the unit and have a class method that refers to
it, as Chad Z. Hower suggested.  Or, if the thing always stays the same and
you know what its value should be, I guess you could hardcode the value in
the method!

Other Threads