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 1 2745
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();
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Marcin Vorbrodt |
last post by:
So I have a class Math that looks like this:
Math {
public:
static Real PI(void);
};
Real Math::PI(void) {
return 4.0 * atan(1.0);
}
|
by: cppaddict |
last post by:
I would like to write, within the private section of my class
definition:
static enum TruncType {TRUNC_TOP,TRUNC_RIGHT,TRUNC_BOTTOM,TRUNC_LEFT};
but C++ does not allow this. The four values above are constant and
static, and will be used only by a private class method.
I could just do:
|
by: Bret Pehrson |
last post by:
I just stumbled across the following problem:
//.h
class Masses
{
static double mass1;
static double mass2;
static double mass3;
};
|
by: John Ratliff |
last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave,
it's a standard C++ question, I promise.
This program will compile and link fine under mingw/g++ 3.4.2, but fails
to link under Linux/g++ 3.3.3.
---------------------------------------------
#include <iostream>
#include <utility>
|
by: dual0 |
last post by:
Hello,
I found some function like
void static foo(...){
....
}
what does the static keyword stand for? what is a static function?
| |
by: Edson Tadeu |
last post by:
I was thinking in a way to do static dispatching on enumerations, in a
way similar to dispatching on integral constants using Loki's
Int2Type<> or Boost.MPL's int_<>, i.e, creating types based on the
enumeration constants, so I came up with this example code:
#include <iostream>
using namespace std;
template <class EnumType>
|
by: Steve Folly |
last post by:
Hi,
I had a problem in my code recently which turned out to be the 'the "static
initialization order fiasco"' problem
(<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>)
The FAQ section describes a solution using methods returning references to
static objects.
But consider:
|
by: Ranganath |
last post by:
Hi,
Why is there a restriction that only integral types can be made static
constant members of a class?
For e.g.,
class B {
private:
static const double K = 10;
};
|
by: aaragon |
last post by:
Hi everyone,
I have a linking error when using gcc4.2 and static member variables.
The class template definition is something around the following:
template<>
class Element<L2_t: public Element_common<L2, Side<2,2 {
public:
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |