Solved

static member variables

Posted on 2002-03-31
5
202 Views
Last Modified: 2010-04-02
The following code fragment compiles fine with MSVC++ 6.

-- AlienShip.h

class AlienShip : public Sprite  
{
...
private:
     static HBITMAP m_hSprite;
...
};

-- AlienShip.cpp

#include "AlienShip.h"

HBITMAP AlienShip::m_hSprite = NULL;
...

Later, I load a bitmap with the help of the bitmap handler, and use this static variable -m_hSprite- throughout my program. (In fact, this is irrelevant, it's just supposed to give you an idea.)

Now, I'm thinking of doing an animation, and I need 7 bitmap handlers. I couldn't find anything said on static member variable arrays, so tried the following:

-- AlienShip.h

class AlienShip : public Sprite  
{
...
private:
     static HBITMAP m_hSprite[7];
...
};

-- AlienShip.cpp

#include "AlienShip.h"

HBITMAP AlienShip::m_hSprite[0] = NULL;
...


and received the following error message: error C2466: cannot allocate an array of constant size 0.
I tried some other combinations hoping that they would work, but no.

So, the point is, can I do what I want? If yes, how? Please note that I need to use static.
0
Comment
Question by:kushcu
  • 2
  • 2
5 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 120 total points
ID: 6909453
Try the following:

HBITMAP AlienShip::m_hSprite[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
0
 
LVL 86

Expert Comment

by:jkr
ID: 6909495
In other words (and I think that is what Axter wanted to say): You'd better be sure to actually initialize a static mamber...
0
 

Author Comment

by:kushcu
ID: 6909904
Axter, thanks! Could you also please tell me why it doesn't work the other way around? (some simple comment would help.)
0
 
LVL 30

Expert Comment

by:Axter
ID: 6910114
The class declaration is declaring an array of 7, but the variable initiation is declaring an array of zero.

Just the fact that it's zero will make the compile fail to compile the code.

Example:
int main(int argc, char* argv[])
{
 char xyz[0] = NULL;//This line fails to compile
 char xyz2[0];//This line also fails to compile
 return 0;
}

Now if you wanted to just initiate the first item in the array, you could still do that, as long as the array is more then zero, and it matches the class declaration.
Example:
HBITMAP AlienShip::m_hSprite[7] = {NULL}; //Use brackets for an array.

0
 

Author Comment

by:kushcu
ID: 6910362
OK, that clears up the issue for me. Thanks for the help.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 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