Solved

static member variables

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parsing XML instructions in Java/any other language.... 3 104
getting a fire fox page (view page source ) in c++ 6 42
C++ Language error 28 186
max float value 3 20
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 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.

948 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

18 Experts available now in Live!

Get 1:1 Help Now