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?
  
User name:
Password:
Reply:
 

UserPost
NightHawk
Posts: 151/621
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
Emptyeye
Posts: 208/2273
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.
Acmlm
Posts: 749/1173
"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 )
NightHawk
Posts: 149/621
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?
Emptyeye
Posts: 206/2273
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!
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.011 seconds.