   const static HPEN inheritance problem
  I have a base object with a member:static HPEN redPen; then I have another member:HPEN currentPen I also have some other colored pens that I initialize in my constructor, which currently are not static, including a black and green ones.  All of my code for the entire app is Win32 API.Now, I have two object derived from the base object and in the constructor for each of these objects I do:currentPen = greenPen; // or currentPen = redPen; depending on the object.Now, initially I had all the pens in the base class declared as just regular protected data members and my program worked properly.  As soon as I moved one of the pens to be static ALL of the derived objects become the same color as what I initialize currentPen to in the base class.  I can't seem to change the currentPen in either of my derived classes.  It's not const nor static and I can't figure out why this would be.What I am hoping to do is set up my base class to initialize all the pens I would need, so none of that data is duplicated when I instantiated all of my objects derived from it.  The only pen I would need to set is the currentPen.I hope that is clear.[edited by - Sanadan on July 31, 2003 12:03:04 AM][edited by - Sanadan on July 31, 2003 12:04:04 AM]
  bump - no one knows? or was my question worded poorly (seems ok to me). ;  A static class member because a member of the class.  Meaning that it doesn''t belong to single instance of the class, but to ALL members of the class.That could be your problem, but with more code I can''t be sure. ;quote:Original post by Anonymous Poster  A static class member because a member of the class.  Meaning that it doesn''t belong to single instance of the class, but to ALL members of the class.That could be your problem, but with more code I can''t be sure.   Well the thing is lets say we have a classclass Object{  public:  static int x;  int y;}int Object::x=60;if you make 2 instances like thisObject obj1, obj2;so obj1 and obj2 will contain the value 60 because thats what static does, it makes all instances derived from the same object share that member variable.the x member variable will be shared by both obj instances. But of course the y member variable won''t be shared by both instances so you can assign them both diff values and they''ll store them   no theory` ;  from memory (don't have my code available):I'll try to include everything relevant, remember, I'm rewriting from scratch hereI don't think I have hte problem you two are discussing. class Class1 {  public:    Class1(HWND = NULL, HDC = NULL, int = 0);    void Draw() const;    ...  protected:    ...    HWND hwnd;    HDC hdc;    int numVertices;    static HPEN greenPen; // I will also make this const but for now, it don't matter    HPEN redPen, blackPen, currentPen, oldPen; // will also make const later    // brushes, and some more pens, but irrelevant atm.};// cpp fileHPEN greenPen = CreatePen(PS_SOLID, 1, RGB(0,255,0));Class1::Class1(HWND h, HDC backBuff, int numVert)// i do some stuff with my initialization list, but it isn't relavent and I don't have my code atm{   ... // some initialization   hwnd = h;    hdc = backBuff;   redPen = CreatePen(PS_SOLID, 1, RGB(255,0,0));   blackPen = CreatePen(PS_SOLID, 1, RGB(0,0,0));   currentPen = redPen; // or I can set to greenPen, it doesn't matter, I just want to initialize   oldPen = (HPEN)SelectObject(hdc, currentPen); // assume this line is correct even though syntax may be wrong atm.   ... // some other code, brushes and such, initialization}void Class1::Draw() const {   // erase object   SelectPen(hdc, blackPen);   Polygon(hdc, object, numVert);   // draw object in new position   SelectPen(hdc, currentPen);   Polygon(hdc, object, numVert);}// NEW CLASS in header file and cppclass Class2 : public Class1 {  public:    Class2(HWND = NULL, HDC = NULL);    // some functions, moving, etc.    ...  protected:    ... // stuff};Class2::Class2(hwnd h, hdc back) : Class1(h,back,6) // and other initialization stuff{   ... initialization code   currentPen = redPen;}// NEW CLASS in header file and cppclass Class3 : public Class1 {  public:    Class3(HWND = NULL, HDC = NULL);    // some functions, moving, etc.    ...  protected:    ... // stuff};Class3::Class3(hwnd h, hdc back) : Class1(h,back,3) // and other initialization stuff{   ... initialization code   currentPen = greenPen;}// driver cpp file ... Class2 c2(h,backBuffer); Class3 c3(h,backBuffer); c2.Draw(); c3.Draw();// both objects turn out to be the same color, even though only one HPEN is static, and if I// chage that pen back to be non-staic c2 and c3 will be different colors, which is what I wnat// ok, i wrote teh above in notepad, so hope it is formatted ok.   [edited by - Sanadan on August 1, 2003 12:23:39 PM] [edited by - Sanadan on August 1, 2003 12:24:33 PM] [edited by - Sanadan on August 1, 2003 12:25:07 PM]