Click to See Complete Forum and Search --> : about static data member


Serene
08-15-2002, 09:18 PM
#include <iostream.h>

class Student
{
public:
static int noOfStudents;
Student &nextStudent()
{
noOfStudents++;
return *this;
}

};
int Student::noOfStudents;

void fn(Student &s)
{
cout<<s.nextStudent().noOfStudents<<endl;
}

void main()
{
Student ss;
cout<<(ss.noOfStudents)<<endl;
fn(ss);
(Student::noOfStudents)=+18;
clog<<Student::noOfStudents<<endl;

}

I have compiled it and ran it when I called "s.nextStudent().noOfStudents" the noOfStudent return value is 1.if not value is 0,this is okay.but when "(Student::noOfStudent)=+18,the value is 18,(I thought should be 19),I remark it with //(St......),the output is 1.I could understand why? Static vary changed its value ,the value will be changed and kept.so 1+18 should be 19.

mingshun
08-15-2002, 09:49 PM
Originally posted by Serene

void main()
{
Student ss;
cout<<(ss.noOfStudents)<<endl;
fn(ss);
(Student::noOfStudents)=+18;
clog<<Student::noOfStudents<<endl;

}

I have compiled it and ran it when I called "s.nextStudent().noOfStudents" the noOfStudent return value is 1.if not value is 0,this is okay.but when "(Student::noOfStudent)=+18,the value is 18,(I thought should be 19),I remark it with //(St......),the output is 1.I could understand why? Static vary changed its value ,the value will be changed and kept.so 1+18 should be 19.

I am not very familiar in C++ and I don't know what clog does ...

But afaik, your (Student::noOfStudent) is being assigned to 18 first before it is incremented. (prefix op) Hence if you println at the wrong place, you get 18. Try (Student::noOfStudent) = (Student::noOfStudent) + 18; to prove me wrong :p

Serene
08-15-2002, 10:00 PM
Thankz,I made a mistake I thought i=+5 namely i=i+5;but that is i+=5. U are right. thankz

Serene
08-15-2002, 10:03 PM
clog is iostream,but it support buffering like cerr(like c stderr)!