Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Help with a tutorial? | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Emptyeye
I am a real American!
Real American
Level: 67

Posts: 206/2273
EXP: 2488421
For next: 104451

Since: 05-24-04
From: I DUNNOOOOOOOO!!

Since last post: 9 hours
Last activity: 4 hours
Posted on 06-12-04 08:40 PM Link | Quote
Okay, yeah, I know, learning in general with a tutorial is not a good idea. But I'm pretty sure I've gotten as far as I'm going to in college, and the book isn't a great help either, so...

I'm at the section on classes (I learned everything up to that in school...the tutorial in question can be found here, by the way, my question pertains to section 4.2). Specifically, static members. The following code, which I'll attempt to comment with my understanding of it, has me somewhat baffled:

// static members in classes
#include <iostream.h>

class CDummy { //Creation of class
public: //Declaration as public
static int n; //Declaration of static member n within the class
CDummy () { n++; }; //Seems to be an incrementing constructor
~CDummy () { n--; }; //Seems to be a decrementing destructor
};

int CDummy::n=0; //declaration of the variable as 0

int main () {
CDummy a; //Makes a a valid class parameter, increments (See above) (1)
CDummy b[5]; //Makes b a valid class parameter, increments again (See above) (2)
CDummy * c = new CDummy; //?? Seems to create a new instance of the class with a pointer c
cout << a.n << endl; //outputs n within a, which should be 7 according to the code, how?
delete c; //Deletes the above created instance; presumably decrements the n as well
cout << CDummy::n << endl; //?? I read this as "outputs n within CDummy", which is probably wrong. This should be 6.
return 0;
}

So where I need help with is getting in my head how what is output equals 7 and 6, respectively. Once I get that, I should be okay.

Thanks!


(edited by Emptyeye on 06-12-04 11:41 AM)
NightHawk

Bob-Omb
Level: 39

Posts: 149/621
EXP: 374743
For next: 30028

Since: 03-26-04
From: Switzerland

Since last post: 432 days
Last activity: 339 days
Posted on 06-12-04 09:33 PM Link | Quote
Originally posted by Emptyeye
CDummy a; //Makes a a valid class parameter, increments (See above) (1)
At this point, n == 1.


CDummy b[5]; //Makes b a valid class parameter, increments again (See above) (2)
After this, it equals 6 (it initializes 5 instances of the class, so n gets incremented 5 times).


CDummy * c = new CDummy; //?? Seems to create a new instance of the class with a pointer c
Yes, it creates a new instance, which c then points to. Also increments n. n now equals 7.


delete c; //Deletes the above created instance; presumably decrements the n as well
Yes, it decrements n. n == 6.


So where I need help with is getting in my head how what is output equals 7 and 6, respectively. Once I get that, I should be okay.

Thanks!
I think your problem might be that a static member of a class is shared between all instances of that class. Does that explain it to you?
Acmlm

Torosu
heh
Level: 51

Posts: 749/1173
EXP: 981994
For next: 31944

Since: 03-15-04
From: Somewhere that isn't outside of Sherbrooke, Québec, Canada

Since last post: 39 days
Last activity: 3 hours
Posted on 06-12-04 09:33 PM Link | Quote
"CDummy b[5];" should create 5 CDummy variables, incrementing the value by 5 ... while "CDummy * c = new CDummy;" increments by 1, so you get 1 + 5 + 1 = 7, or at least that's how I see it

And then "delete c" calls the destructor and decrements the value, so you get 6 ...

n is shared by all instances of the class, since it's static, and keeps track of how many instances are in memory


(I'm 40 seconds late )


(edited by Acmlm on 06-12-04 12:35 PM)
Emptyeye
I am a real American!
Real American
Level: 67

Posts: 208/2273
EXP: 2488421
For next: 104451

Since: 05-24-04
From: I DUNNOOOOOOOO!!

Since last post: 9 hours
Last activity: 4 hours
Posted on 06-12-04 10:59 PM Link | Quote
Okay, I get it now. Thanks.

I read the b[5] line as "assigns 5 to the b parameter" or "makes parameter b an array 5 characters long" as opposed to "initializes and increments 5 times". The way the two of you explained it makes sense now.
NightHawk

Bob-Omb
Level: 39

Posts: 151/621
EXP: 374743
For next: 30028

Since: 03-26-04
From: Switzerland

Since last post: 432 days
Last activity: 339 days
Posted on 06-12-04 11:27 PM Link | Quote
Originally posted by Emptyeye
Okay, I get it now. Thanks.

I read the b[5] line as "assigns 5 to the b parameter" or "makes parameter b an array 5 characters long" as opposed to "initializes and increments 5 times". The way the two of you explained it makes sense now.
I'm glad I could help
I remember what it was like to learn C++, so you have my sympathies
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Help with a tutorial? | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.005 seconds.