[Scummvm-devel] Re: CVS: scummvm/queen display.cpp,1.41,1.42
Max Horn
max at quendi.de
Wed Dec 31 06:20:04 CET 2003
Am 31.12.2003 um 13:53 schrieb Torbj?rn Andersson:
> Update of /cvsroot/scummvm/scummvm/queen
> In directory sc8-pr-cvs1:/tmp/cvs-serv12521
>
> Modified Files:
> display.cpp
> Log Message:
> There's that incomprehensible MinGW GCC warning again!
>
>
I assume the warning you refer to is about the declaration order of the
member vars not matching the order they are inited in the constructor?
If so, let me shed a little light on the warning (which BTW can be
enabled in any 3.x GCC). It's not completely without merit, although
for us so far it *is* pointless.
The warning does have a good justification once you consider that
member vars may be complicated objects with constructors of their own.
And if those constructors have global effects, they may very well
affect each other. A simple case would be were one sets global variable
g_foo to 0, while the other sets g_foo to 1. Depending on the init
order, at the end g_foo may have either value. Consider this example:
int g_foo = -1;
class Foo0 {
public:
Foo0() { g_foo = 0; }
};
class Foo1 {
public:
Foo1() { g_foo = 1; }
};
class InitOrder {
Foo0 foo0;
Foo1 foo1;
public:
InitOrder() : foo1(), foo0() {}
};
Now I write this in my code:
...
InitOrder test;
...
Bonus question: Which value does g_foo have afterwards? :-)
Answer: 1. The order the objects are constructed is defined by the
order they are declared in the InitOrder class declaration, and not by
the order you put the call to the constructors in the InitOrder
constructor! This surprises many (even pro) C++ coders.
While the above example with g_foo is quite harmless, it's very easy to
construct scenarios where this leads to very real failure, from
immediate crashes to very subtle, hard to track down bugs.
Hence, warning the programmer about this is not the worst thing to do.
However, as long as all involved variables are simple
int/float/double/char* etc. variables, it's is indeed not helpful.
I hope that clarifies the matter a bit!
Cheers,
Max
More information about the Scummvm-devel
mailing list