Article View: borland.public.cppbuilder.oodesign
Article #4Re: Counting the number of instances of a user defined class
From: Maynard Philbrook
Date: Wed, 09 Jul 2003 01:58
Date: Wed, 09 Jul 2003 01:58
36 lines
2099 bytes
2099 bytes
usually you can use a static type and set it..
static in InstanceCount = 0;
that will simlpy force the compiler to generate a static variable in the
table
of static variables and thus be defind at the file level.
in your constructor you simply ++ ect...
Pang Fu wrote:
> How to count how many instances are created of a user defined class?
>
> In the following example how TMyClass can count the number of instances
> (objects created from TMyClass) ?
>
> class TMyClass
> {
> private :
> static int InstanceCount;
> public :
> TMyClass(){
> InstanceCount ++; <--?? How to start from zero and increment?
> }
> ~TMyClass();
> };
> ...
> const int N= 456;
> for (int i=0; i<N; i++){
> TMyClass *MyObj = new TMyClass();
> }
>
> AnsiString S;
> S.sprintf("Instance Count :%d", ????How????);
>
> Pang Fu
> nospam_pangfu@hotmail.com
Technical Details
Message-ID:
<3F0B76D1.486DD6E8@mindspring.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!elnk-pas-nf2!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!harp.news.atl.earthlink.net!not-for-mailReferences:
<3f0a59ff@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10db1e,bcc8bd10d54dd071
X-Google-Attributes: gid10db1e,public
X-Google-Thread: 102541,bcc8bd10d54dd071
X-Google-Attributes: gid102541,public
X-Google-ArrivalTime: 2003-07-08 18:33:15 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-01!sn-xit-08!supernews.com!newsfeed.news2me.com!elnk-pas-nf2!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!harp.news.atl.earthlink.net!not-for-mail
From: Maynard Philbrook <"UseTheFollowing->jamie12"@mindspring.com>
Newsgroups: borland.public.cppbuilder.oodesign,borland.public.cppbuilder.students
Subject: Re: Counting the number of instances of a user defined class
Date: Tue, 08 Jul 2003 21:58:42 -0400
Organization: PC BIT Software
Lines: 37
Message-ID: <3F0B76D1.486DD6E8@mindspring.com>
References: <3f0a59ff@newsgroups.borland.com>
Reply-To: "UseTheFollowing->jamie12"@mindspring.com
NNTP-Posting-Host: 42.a8.28.eb
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Server-Date: 9 Jul 2003 01:33:12 GMT
X-Mailer: Mozilla 4.5 [en]C-CCK-MCD {TLC;RETAIL} (Win98; U)
X-Accept-Language: en,ja