static constants...

  • Thread starter John David Ratliff
  • Start date
J

John David Ratliff

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
 
G

Gianni Mariani

John said:
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();
}

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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top