
write to static class member variable...
Basically, you have to put the instantiation in the cpp file and not the
header. Here is an example of a static count variable that counts how
many instances are created of a class...
-Brian
teststatic.h
--------------------
class TestStatic {
public:
TestStatic();
~TestStatic();
static int count;
Quote:
};
//int count=0; // Cannot do this or you will get link errors
teststatic.cpp
--------------------
#include "TestStatic.h"
int TestStatic::count=0;// Here is where the static member goes
TestStatic::TestStatic() {
count++;
Quote:
}
TestStatic::~TestStatic() {
count--;
Quote:
}
main.cpp
--------------------
#include <stdio.h>
#include "TestStatic.h"
int main(int argc, char* argv[]) {
TestStatic *ts = new TestStatic();
printf("Count: %d\n",ts->count);
TestStatic *TS = new TestStatic();
printf("Count: %d\n",ts->count);
printf("Count: %d\n",TS->count);
delete ts;
printf("Count: %d\n",TS->count);
delete TS;
return 0;
Quote:
}
--
Brian Rectanus
Quote:
> Hi all,
> I need to assign different values to a static variable of a
> class. The following code do not work:
> Set.h
> class CSet
> {
> public:
> ......
> static int count ;
> protected:
> ........
> };
> int CSet::count = 0; // Is this line neccessary??
> CSetView.cpp
> ............
> if ( type = = "DS")
> CSet::count = 0;
> .....
> The error message i got was :
> unresolved external symbol "public: static int CSet::count"
> can someone tell me how to write to a static member?? I try declaring a
> static member function and do the assignment inside it....and the same
> problem still arise....i also try using friend static function and it
> still won't work. btw, i am using VC++ 6.0
> pls e-mail me in addition to the newsgroup.
> Thanks in advance....
> leesze
> --
> ********************************************************
> *As Imagination Bodies Forth
> *The Forms Of Things Unknown, The Poet's Pen
> *Turns Them To Shapes, And Gives To Airy Nothing
> *A Local Habitation And A Name
> *
> ********************************************************