Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

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.



 }
0
e6694811
Asked:
e6694811
  • 8
  • 5
  • 4
  • +1
1 Solution
 
ghimirenirajCommented:
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
 
e6694811Author Commented:
>>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
 
RONSLOWCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
e6694811Author Commented:

  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
 
ShaunWildeCommented:
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
 
e6694811Author Commented:

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

 I think rronslow 's way will not work (see my earlier message)
0
 
ShaunWildeCommented:
it will but only if you let CMyClass know about CMyDoc
0
 
RONSLOWCommented:
in your cpp for myclass, you'd have
#include "mydoc.h"


0
 
e6694811Author Commented:
I see .What if I include # "mydoc.h" in .* h instead ?
0
 
RONSLOWCommented:
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
 
e6694811Author Commented:
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
 
ShaunWildeCommented:
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
 
e6694811Author Commented:
Adjusted points from 55 to 65
0
 
RONSLOWCommented:
Nice of you to bump up the points .. are you going to accept my answer?
0
 
e6694811Author Commented:
RONSLOW ,I dindn't  receive your last message.That's why I didn't come back sooner, sorry.
0
 
RONSLOWCommented:
s'ok.  Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now