Solved

static member variables

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

860 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