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
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