Archived

This topic is now archived and is closed to further replies.

Initializing array in class

This topic is 5652 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I go about initializing a array during startup in a class. It's a big array that's values are keyed in by hand like so: const BYTE Palette[256][3] = {{0,0,0}, {15,15,15}, ect..} I found this in the forums: http://www.gamedev.net/community/forums/topic.asp?topic_id=60157 Stoffel talks about making it static, but I have a problem with my variable still going out of scope after I declare it static. [edited by - MagTDK on June 23, 2002 2:09:57 PM]

Share this post


Link to post
Share on other sites
If there is any form of logical progression to the palette colors, then fill them in that way (write a function that implements said logic). If not, either fill them in by hand in the constructor or zero the array in the constructor and provide a separate initialization function.

Lastly, if you decide to make it static (class) data, then use reference counting or an internal flag to know when to set it up and when to tear it down.

Share this post


Link to post
Share on other sites
If I declare this in the class like so: BYTE Palette[256][3];

What's the proper syntax to initialize them in the constructor?

When I do this: Palette[256][3] = {{0,0,0}, {15,15,15}, ect..} in the constructor I get a bunch of syntax errors.

[edited by - MagTDK on June 23, 2002 2:59:42 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by MagTDK
Which leads me back to, how do I make my array global within my class so other class functions can access them?

Which leads us back to using a static member. I''d suggest you use a vector or some sort of class wrapping the array rather than a bare array, and then populate it on the first ctor call. Something like:


  
#include <vector>

class C
{
public:
C()
{
if(!initialized_)
{
v_.push_back(1);
v_.push_back(2);
v_.push_back(3);
initialized_ = true;
}
}
private:
static std::vector<int> v_;
static bool initialized_;
};
std::vector<int> C::v_;
bool C::initialized_ = false;


Is that the sort of thing you''re trying to get at?

Share this post


Link to post
Share on other sites