Link to home
Start Free TrialLog in
Avatar of e6694811
e6694811

asked on

Problem with static attribute


 Hi ,there.

 I'm developing a MDI application. Consider the following class:

 class CMyClass:public CObject
 {
   protected:
    static UINT m_numberOfObjects;
    UINT m_number;
.......

  public:
   CMyClass()
  {
   m_number =++m_numberOfObjects ;
  }

  ....

 }

 Every document opened handles a list of these objects.
 
 As you may note ,m_numberOfObjects stores the amount of objects
 of the same class .Its value should be increased whenever a new
 object is created.Similarly m_number stores the number of the
 object recently created.

 This approach is Ok if there is only one one document opened .My
 headache begins when I open another document :I want the variable to be
 initialized to "0"  because I'm working with another document and therefore
 with another ,initially empty list ,   .The problem is
 that  it does not occur :since MyClass.h file is shared by all the documents
 it doesn't store the number of the last object in the current document but
 the number of the last object in all the docs.

  Any idea to solve this problem ? (I hope you know what I mean)  

 Regards.



 }
Avatar of ghimireniraj
ghimireniraj

Static variables are created only once
and remain throughout the life cycle of an application.

You can's create it twice ,so i guess you remove static attribute.

Why do you want to use static there, i mean for what benefit?
Avatar of e6694811

ASKER

>>Why do you want to use static there, i mean for >>what benefit?


 As I said ,every element has a correlative ID
number .So ,the static variable stores the
number of the last object created.(See the
constructor)
make it a member (non-static) of the document class instead.

pass a pointer to the document to the construtor of your object.

class CMyClass:public CObject {
  protected:
    UINT m_number;
  ....
  public:
    CMyClass(CMyDocument* pDoc)
    {
      m_number = pDoc->GetNextNumber();
    }
  ....
};

class CMyDocument:public CDocument {
  public:
    UINT m_nextnumber;
    UINT GetNextNumber() {
      return ++m_nextnumber;
    }
  ...
  CMyDocument()
  : m_nextnumber(0)
  ... {
    ...
  }
};


  I only see a problem :how does CMyClass
 know about pDoc ? .Bear in mind that at the very
 top of "CMyDoc.h" will be included the include sentence: "#include "CMyClass.h"

 Regards.
I agree with RONSLOW - or something which is a variation on the same theme.

How about keeping a counter in your document and then have a different constuctor which assigns the value to your class

class CMyClass
{
private:
CMyClass();
public:
CMyClass(UINT nCount)
{
m_nNumber=nCount;
}
protected:
UINT m_nNumber;
};

then from your document

CMyDoc
{
public:
CMyDoc()
{
m_nCounter=0;
}
void CreateMyClass();
protected:
UINT m_nCounter;

};

void CMyDoc::CreateMyClass()
{
CMyClass *pClass=new CMyClass(m_nCounter++);
....
}


That way myclass doesn't know about CMyDoc

Well ,it's a possible solution. I wouldn't have this problem if my application was SDI ,rigth ?
yup - if you wished to use statics - however mine (or ronslows) way would allow you to do both :)

 I think rronslow 's way will not work (see my earlier message)
it will but only if you let CMyClass know about CMyDoc
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I see .What if I include # "mydoc.h" in .* h instead ?
You could do that .. but it is generally better programming practice to use forward declarations in your .h files whenever possible, and only #include the 'real thing' in your cpp.

eg.
in a .h you can say
  class CMyDoc; // forward declare
  ...
  class X {
    ...
    void f (CMyDoc* pDoc);
    ...
  };
and in your .cpp you would say
  #include "mydoc.h"
  ...
  void X::f (CMyDoc* pDoc) {
    ...
    pDoc->g(); //you can now dereference pDoc
    // because you have #included mydoc.h
    ...
  }

This reduces dependancies in your code, and means less recompilation when changes happen and quicker compilation (because there are less #includes to process) and avoids circular includes etc.  In other words there are LOT of good reasons to avoid #includes in other #includes unless it is absolutely necessary.

Just one more thing : do all the forward declarations work always properly ? I remember myself adding some of those forward declarations
in a *.h file and getting the following error message : class X has not a container class (or similar)
forward declaration only work when you wish to specify a pointer of that class - you will need to the include the header if you wish to access methods or members of the class
Adjusted points from 55 to 65
Nice of you to bump up the points .. are you going to accept my answer?
RONSLOW ,I dindn't  receive your last message.That's why I didn't come back sooner, sorry.
s'ok.  Thanks.