Solved

Problem with static attribute

Posted on 2000-04-14
18
171 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 113
Doc'in system (example?) BA 7 107
Modbus - whats the maximum I can store in one register? 4 79
Excel file not created as expected 7 71
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

775 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