Board index » delphi » OOP in D1: C++ static variable in class

OOP in D1: C++ static variable in class

Is there a way to make class variable in D1?

        I have tried to use a typed constant, but it will not allow me to nest
it within the class.  And if I just declare it as part of the interface,
I can't make the function, that adjusts this common proporty to all
instances, part of the class, because if it is to be changed it should
be able to be changed before instanting.

  I am rather new to delphi, but have OOP quite extensively in C++ and
an frustrated with Object Pascal.

Can anyone suggest a good D1 or D+ OPP book, I have two fat manuals and
both gloss over it, being more concerned with VLC.

Nuff said,
  Frustrated Lloyd.

 

Re:OOP in D1: C++ static variable in class


Quote
Lloyd Budd wrote:
> Is there a way to make class variable in D1?

>         I have tried to use a typed constant, but it will not allow me to nest
> it within the class.  And if I just declare it as part of the interface,
> I can't make the function, that adjusts this common proporty to all
> instances, part of the class, because if it is to be changed it should
> be able to be changed before instanting.

>   I am rather new to delphi, but have OOP quite extensively in C++ and
> an frustrated with Object Pascal.

> Can anyone suggest a good D1 or D+ OPP book, I have two fat manuals and
> both gloss over it, being more concerned with VLC.

> Nuff said,
>   Frustrated Lloyd.

Duplicating a true static equivalent is really not possible.  Global variables are
about a close as you can get.  If this variable is needed within only on routine
of the class you can hide it by placing it as an initialized variable within the
scope of the routine.  (Note that initialized variable is borland preferred
equivalent to typed constants that are to be modified)

procedure TYourClass.DoSomething;
var
    ClassVariable:integer=0;
begin
    ...
end;

Bob Lee

Re:OOP in D1: C++ static variable in class


Lloyd Budd <lb...@PFC.Forestry.CA> wrote in article
<34C68B0A.3...@PFC.Forestry.CA>...

Quote
> Is there a way to make class variable in D1?

>    I have tried to use a typed constant, but it will not allow me to nest
> it within the class.  And if I just declare it as part of the interface,
> I can't make the function, that adjusts this common proporty to all
> instances, part of the class, because if it is to be changed it should
> be able to be changed before instanting.

>   I am rather new to delphi, but have OOP quite extensively in C++ and
> an frustrated with Object Pascal.

> Can anyone suggest a good D1 or D+ OPP book, I have two fat manuals and
> both gloss over it, being more concerned with VLC.

Sure you can. The Delphi/Pascal's classes hasn't class variables but what
is
class variable? Any simple variable hidden in implementation part of unit
can be
used exact like class wariable in C. All you neet to choise good name for
that variable.
// (C++)
class MyClass {
  public class int Counter;
  private class int Dummy;

Quote
};

// (Delphi/Pascal)
interface
  MyClass = class;
  var MyClass_Counter: Integer;
implementation
  var MyClass_Dummy: Integer;

I hope it's enough.
Dmitry.      

Re:OOP in D1: C++ static variable in class


I just thought of a good way to emulate a static/class variable in Delphi.  Depending
on performance/readability requirements, there are a couple of ways to proceed.  Below
is an example of a class variable of type integer.

interface

TMyClass=class
 private
    function GetStaticInt:integer;
    procedure SetStaticInt(Value:integer);
public
    property StaticInt:integer read GetStaticInt write SetStaticInt;
end;

implementation

var
    TMyClassStaticInt:integer;

function TMyClass.GetStaticInt:integer;
begin
    result:=TMyClassStaticInt;
end;

procedure TMyClass.SetStaticInt(Value:integer);
begin
    TMyClassStaticInt:=Value;
end;

end.

Alternatively, you could declare the Get/Set routines as virtual so that descendent
classes could override and have their own variable.

A slightly cleaner (but slower) version would be to use a third function to get the
variable that get/set use and which could contain the actual variable declaration

interface

TMyClass=class
 private
    function StaticIntAddress:Pointer; virtual;
    function GetStaticInt:integer;
    procedure SetStaticInt(Value:integer);
public
    property StaticInt:integer read GetStaticInt write SetStaticInt;
end;

implementation

function TMyClass.StaticIntAddress:Pointer;
var
    StaticInt:integer=0;
begin
    result:=@StaticInt;
end;

function TMyClass.GetStaticInt:integer;
begin
    result:=Integer(StaticIntAddress^);
end;

procedure TMyClass.SetStaticInt(Value:integer);
begin
    Integer(StaticIntAddress^):=Value;
end;

end.

There are other variations on this theme.

Bob Lee

Re:OOP in D1: C++ static variable in class


Quote
Dmitry wrote:

> Lloyd Budd <lb...@PFC.Forestry.CA> wrote in article
> <34C68B0A.3...@PFC.Forestry.CA>...
> > Is there a way to make class variable in D1?
> Sure you can. The Delphi/Pascal's classes hasn't class variables but what
> is
> class variable? Any simple variable hidden in implementation part of unit
> can be
> used exact like class wariable in C. All you neet to choise good name for
> that variable.
> // (C++)
> class MyClass {
>   public class int Counter;
>   private class int Dummy;
> };

> // (Delphi/Pascal)
> interface
>   MyClass = class;
>   var MyClass_Counter: Integer;
> implementation
>   var MyClass_Dummy: Integer;

> I hope it's enough.
> Dmitry.

Maybe I'm being retarded but wouldn't that create a new instance of the
var for each object.  As Robert Lee suggested I am begrudgedly using
global vars with propertys and functions to clean up the interface

Re:OOP in D1: C++ static variable in class


Quote
Robert Lee wrote...

|I just thought of a good way to emulate a static/class variable in
Delphi.  Depending
|on performance/readability requirements, there are a couple of ways
to proceed.  Below
|is an example of a class variable of type integer.
|
|interface
|
|TMyClass=class
| private
|    function GetStaticInt:integer;
|    procedure SetStaticInt(Value:integer);
|public
|    property StaticInt:integer read GetStaticInt write SetStaticInt;
|end;
|
|implementation
|
|var
|    TMyClassStaticInt:integer;
[snip]
|There are other variations on this theme.
|
|Bob Lee
|

I might be missing something here, but isn't the following much
simpler ?
Type
  TMyClass = Class()
  ...
  ...
  end;
IMPLEMENTATION
var
  MyClassVar1 : TMyType1;
  MyClassVar2 : TMyType2;
  etc...
then, eventually:
INITIALIZATION
 MyClassVar1 := ...
 MyClassVar2 := ...
end.

That way MyClassVar1/2/.. are common to all instances of TMyClass
 and, yes, to all other classes eventually defined in the same unit).
Why do it the more elaborate way you propose? Besides from the added
complexity, several more methods would be needed to deal with all
possible var types.
Again, If I'm missing the point, I would like to read or hear about
it.

Regards,
Dirk Claessens
______________________________________________
Home   : <dirk.claess...@village.uunet.be>
Office : <dirk.claessens...@belgium.agfa.com>
Nothing will come of nothing.
Dare mighty things. (William Shakespeare)
______________________________________________

Re:OOP in D1: C++ static variable in class


: "Dirk Claessens" <Dirk.Claess...@village.uunet.be> wrote:

Quote

>Robert Lee wrote...
>|I just thought of a good way to emulate a static/class variable in
>Delphi.  Depending
>|on performance/readability requirements, there are a couple of ways
>to proceed.  Below
>|is an example of a class variable of type integer.
>|
>|interface
>|
>|TMyClass=class
>| private
>|    function GetStaticInt:integer;
>|    procedure SetStaticInt(Value:integer);
>|public
>|    property StaticInt:integer read GetStaticInt write SetStaticInt;
>|end;
>|
>|implementation
>|
>|var
>|    TMyClassStaticInt:integer;
>[snip]
>|There are other variations on this theme.
>|
>|Bob Lee
>|

>I might be missing something here, but isn't the following much
>simpler ?
>Type
>  TMyClass = Class()
>  ...
>  ...
>  end;
>IMPLEMENTATION
>var
>  MyClassVar1 : TMyType1;
>  MyClassVar2 : TMyType2;
>  etc...
>then, eventually:
>INITIALIZATION
> MyClassVar1 := ...
> MyClassVar2 := ...
>end.

>That way MyClassVar1/2/.. are common to all instances of TMyClass
> and, yes, to all other classes eventually defined in the same unit).
>Why do it the more elaborate way you propose? Besides from the added
>complexity, several more methods would be needed to deal with all
>possible var types.
>Again, If I'm missing the point, I would like to read or hear about
>it.

[my apologies for the huge quote]

I wonder what is different in your code as opposed to Robert's?

--
mailto:Stefan.Hoffmeister (at) Uni-Passau.de
http://kakadu.rz.uni-passau.de/~w4hoff01/
   DIR: Delphi FAQs, KBs, docs
   PGP public key at homepage

Re:OOP in D1: C++ static variable in class


Quote
Dirk Claessens wrote:
> I might be missing something here, but isn't the following much
> simpler ?
> Type
>   TMyClass = Class()
>   ...
>   ...
>   end;
> IMPLEMENTATION
> var
>   MyClassVar1 : TMyType1;
>   MyClassVar2 : TMyType2;
>   etc...
> then, eventually:
> INITIALIZATION
>  MyClassVar1 := ...
>  MyClassVar2 := ...
> end.

> That way MyClassVar1/2/.. are common to all instances of TMyClass
>  and, yes, to all other classes eventually defined in the same unit).
> Why do it the more elaborate way you propose? Besides from the added
> complexity, several more methods would be needed to deal with all
> possible var types.
> Again, If I'm missing the point, I would like to read or hear about
> it.

> Regards,
> Dirk Claessens
> ______________________________________________

First off, I was trying to recreate the equivalent of a static class
variable not a hidden variable that the class knew about.  Secondly,
there are lots of variations on the same theme, such as yours.  So it's
more a matter of deciding whats right for you.

Bob Lee

Other Threads