static constants...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John David Ratliff

    static constants...

    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



  • Gianni Mariani

    #2
    Re: static constants...

    John David Ratliff wrote:[color=blue]
    > 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;
    > }
    > };
    >[/color]

    The following objects are constructed dynamically before main is called.
    [color=blue]
    > 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)?[/color]

    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.
    [color=blue]
    >
    > 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?[/color]

    The usual way is to have static methods :

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

    return this_color();
    }


    Comment

    Working...