How many active instances of objects/classes/types.

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
D.J.Peters
Posts: 7825
Joined: May 28, 2005 3:28

How many active instances of objects/classes/types.

Postby D.J.Peters » Mar 27, 2011 8:11

Normaly, so far i know, you can't share a type member with all instances of it
but you can define a static helper function.

Only usefull if you need to know how many active instances exists.
(i need it for thread control inside of a audio engine)

Joshy

Code: Select all

type TESTCLASS
  enum JOBS
    _DEC = -1
    _GET
    _INC
  end enum
  declare constructor
  declare destructor
  declare static function NumberOfInstances(job as JOBS=_GET) as integer
  private:
  as integer dummy
end type
type PTESTCLASS as TESTCLASS ptr

constructor TESTCLASS
  NumberOfInstances _INC
end constructor

destructor TESTCLASS
  NumberOfInstances _DEC
end destructor

function TESTCLASS.NumberOfInstances(job as JOBS) as integer
  static as integer nInstances=0
  nInstances+=job:return nInstances
end function


dim as PTESTCLASS a = new TESTCLASS
with *a : ? .NumberOfInstances : end with
dim as TESTCLASS b,c
with  b : ? .NumberOfInstances : end with
with  c : ? .NumberOfInstances : end with
delete a
with  b : ? .NumberOfInstances : end with
with  c : ? .NumberOfInstances : end with
sleep
D.J.Peters
Posts: 7825
Joined: May 28, 2005 3:28

Postby D.J.Peters » Mar 27, 2011 8:20

two of the 'with' statements inside the code block are in black not in blue ?
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Postby agamemnus » Mar 27, 2011 20:00

Your eyes are just playing tricks on you!

(yes, they are...)

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest