Thread View: borland.public.cppbuilder.oodesign
4 messages
4 total messages
Started by Pang Fu
Tue, 08 Jul 2003 05:42
Counting the number of instances of a user defined class
Author: Pang Fu
Date: Tue, 08 Jul 2003 05:42
Date: Tue, 08 Jul 2003 05:42
26 lines
1669 bytes
1669 bytes
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
Re: Counting the number of instances of a user defined class
Author: Remy Lebeau (TeamB)
Date: Tue, 08 Jul 2003 06:58
Date: Tue, 08 Jul 2003 06:58
53 lines
2515 bytes
2515 bytes
"Pang Fu" <nospam_pangfu@hotmail.com> wrote in message
news:3f0a59ff@newsgroups.borland.com...
> How to count how many instances are created
> of a user defined class?
Add a static member to the class, then increment it in the constructor and
decrement it in the destructor.
> In the following example how TMyClass can count
> the number of instances (objects created from TMyClass) ?
> InstanceCount ++; <--?? How to start from zero and increment?
Static members must be defined separately from the class declaration.
Simply declare the variable to have an initial value of 0 when you define it
in your source file:
--- header file ---
class TMyClass
{
public :
// InstanceCount must be public if you intend to access it directly
otherwise
// you will need to declare a public method to return the value
instead
static int InstanceCount;
TMyClass() { ++InstanceCount; }
~TMyClass() { --InstanceCount; }
};
--- source file ---
int TMyClass::InstanceCount = 0;
> const int N= 456;
> for (int i=0; i<N; i++){
> TMyClass *MyObj = new TMyClass();
> }
You do realize that you have a memory leak there, don't you? You are
allocating 456 instances of the class dynamically, but are never freeing
them.
> AnsiString S;
> S.sprintf("Instance Count :%d", ????How????);
S.sprintf("Instance Count :%d", TMyClass::InstanceCount);
Gambit
Re: Counting the number of instances of a user defined class
Author: 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
Re: Counting the number of instances of a user defined class
Author: Pang Fu
Date: Wed, 09 Jul 2003 13:37
Date: Wed, 09 Jul 2003 13:37
4 lines
1299 bytes
1299 bytes
Gambit, Thank you very much for your help!!! Now, I can count the number of instances correctly.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads