Solved

Problem with static attribute

Posted on 2000-04-14
18
169 Views
Last Modified: 2013-11-20

 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.



 }
0
Comment
Question by:e6694811
  • 8
  • 5
  • 4
  • +1
18 Comments
 
LVL 3

Expert Comment

by:ghimireniraj
ID: 2716007
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?
0
 

Author Comment

by:e6694811
ID: 2716051
>>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)
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2716236
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)
  ... {
    ...
  }
};

0
 

Author Comment

by:e6694811
ID: 2716277

  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.
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2716496
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
0
 

Author Comment

by:e6694811
ID: 2716805

Well ,it's a possible solution. I wouldn't have this problem if my application was SDI ,rigth ?
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2716991
yup - if you wished to use statics - however mine (or ronslows) way would allow you to do both :)
0
 

Author Comment

by:e6694811
ID: 2717110

 I think rronslow 's way will not work (see my earlier message)
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2717159
it will but only if you let CMyClass know about CMyDoc
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 10

Accepted Solution

by:
RONSLOW earned 65 total points
ID: 2721239
in your cpp for myclass, you'd have
#include "mydoc.h"


0
 

Author Comment

by:e6694811
ID: 2722764
I see .What if I include # "mydoc.h" in .* h instead ?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2724967
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.

0
 

Author Comment

by:e6694811
ID: 2726236
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)
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2726306
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
0
 

Author Comment

by:e6694811
ID: 2726652
Adjusted points from 55 to 65
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2729479
Nice of you to bump up the points .. are you going to accept my answer?
0
 

Author Comment

by:e6694811
ID: 2817141
RONSLOW ,I dindn't  receive your last message.That's why I didn't come back sooner, sorry.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2819419
s'ok.  Thanks.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now