By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

static constants...

P: n/a
I'm new to C++ and have a question about static class constants.

Let's say we have two classes

---------------------------------------------
#include <iostream>

using namespace std;

class Color {
float red, green, blue;
public:
static const Color WHITE;
static const Color BLACK;

Color(float red, float green, float blue) : red(red), green(green),
blue(blue) {}
Color() : red((float)1.0), green((float)1.0), blue((float)1.0) {}
float getRed() const { return red; }
float getGreen() const { return green; }
float getBlue() const { return blue; }
};

class App {
public:
static const Color ARRAY[];
static void display() {
Color white = ARRAY[0];
Color black = ARRAY[1];

cout << "white rgb = (" << white.getRed() << "," << white.getGreen() <<
"," << white.getBlue() << ")" << endl;
cout << "black rgb = (" << black.getRed() << "," << black.getGreen() <<
"," << black.getBlue() << ")" << endl;

white = Color::WHITE;
black = Color::BLACK;

cout << "white rgb = (" << white.getRed() << "," << white.getGreen() <<
"," << white.getBlue() << ")" << endl;
cout << "black rgb = (" << black.getRed() << "," << black.getGreen() <<
"," << black.getBlue() << ")" << endl;
}
};

const Color App::ARRAY[] = {Color::WHITE, Color::BLACK};

const Color Color::WHITE = Color();
const Color Color::BLACK = Color(0, 0, 0);

int main(int argc, char *argv[]) {
App::display();
}
-----------------------------------

Why do the first Colors, from the array both print (0,0,0) for the RGB
values, and the second one correctly prints White at (1,1,1)?

In this limited example, it's easy to fix, if I rearrange the definitions so
that the Colors are declared before the array.

But what if I have mutliple files. Color.h, Color.cc, App.h, App.cc

How can I know which one will come first? Is there any way to ensure this?

I tested this example with DJGPP (G++ 3.1), but I am using Visual Studio.
NET 2003.

Is there a build-order I can enforce that will make sure the array is
defined AFTER the colors?

Thanks,

John David Ratliff

Jul 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
John David Ratliff wrote:
I'm new to C++ and have a question about static class constants.

Let's say we have two classes

---------------------------------------------
#include <iostream>

using namespace std;

class Color {
float red, green, blue;
public:
static const Color WHITE;
static const Color BLACK;

Color(float red, float green, float blue) : red(red), green(green),
blue(blue) {}
Color() : red((float)1.0), green((float)1.0), blue((float)1.0) {}
float getRed() const { return red; }
float getGreen() const { return green; }
float getBlue() const { return blue; }
};

class App {
public:
static const Color ARRAY[];
static void display() {
Color white = ARRAY[0];
Color black = ARRAY[1];

cout << "white rgb = (" << white.getRed() << "," << white.getGreen() <<
"," << white.getBlue() << ")" << endl;
cout << "black rgb = (" << black.getRed() << "," << black.getGreen() <<
"," << black.getBlue() << ")" << endl;

white = Color::WHITE;
black = Color::BLACK;

cout << "white rgb = (" << white.getRed() << "," << white.getGreen() <<
"," << white.getBlue() << ")" << endl;
cout << "black rgb = (" << black.getRed() << "," << black.getGreen() <<
"," << black.getBlue() << ")" << endl;
}
};

The following objects are constructed dynamically before main is called.
const Color App::ARRAY[] = {Color::WHITE, Color::BLACK};

const Color Color::WHITE = Color();
const Color Color::BLACK = Color(0, 0, 0);

int main(int argc, char *argv[]) {
App::display();
}
-----------------------------------

Why do the first Colors, from the array both print (0,0,0) for the RGB
values, and the second one correctly prints White at (1,1,1)?
The const Color App::ARRAY[] is constructed before the WHITE and BLACK
objects are constructed but it's constructor is using them.

You need to be very careful because when you have objects in different
files, the order of construction is unpredictable - the right thing to
do is use a construct on reference paradigm.

In this limited example, it's easy to fix, if I rearrange the definitions so
that the Colors are declared before the array.

But what if I have mutliple files. Color.h, Color.cc, App.h, App.cc

How can I know which one will come first? Is there any way to ensure this?


The usual way is to have static methods :

class ...
static const Color & WHITE();
....
const Color & Color::WHITE()
{
static Color this_color();

return this_color();
}
Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.