Solved

static member variables

Posted on 2002-03-31
5
200 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 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

19 Experts available now in Live!

Get 1:1 Help Now