Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with static attribute

Posted on 2000-04-14
18
Medium Priority
?
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

610 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