static variables inside of a class.. possible?
Code:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
using namespace std;
class Counter
{
private:
static int serial;
public:
Counter():serial()
{inc_counter();}
void show_serial()
{
cout << "I am object number " << serial << endl;
}
void inc_counter()
{
serial++;
}
};
int main()
{
Counter s1, s2, s3;
s1.show_serial();
s2.show_serial();
s3.show_serial();
system("PAUSE");
return 0;
}
basically, i want to be able to keep a "serial" number going inside of each object that will display a message saying "i am object number x" (x for whatever number)
the only way i know how to do this is by static, but its not working
any help with the code?
i wanna try to do everything within the class itself. i know i'd be easier if i passed it a number once its constructed, but eh
i think the quesiton is saying for everything to be inside the class