User forums > Help

GetStockObject not working right?

<< < (3/3)

Michael:

--- Quote from: thomas on February 06, 2006, 10:02:41 pm ---
--- Quote from: sethjackson on February 06, 2006, 09:48:55 pm ---True, but C-style casting is hard to spot (most of the time), and not as good.
--- End quote ---
What makes you think that it is "not as good"? It is really the same thing, except that you can do things like
--- Code: ---Foo *foo = new Foo;
Bar *bar = (Bar*) foo;
int a = (int) foo;
printf("%s", (const char*) a);
delete bar; // this one is especially good... :)
int b = 5;
wxWindow *c = (wxWindow*) b;
c->Show();

--- End code ---
with old-style C casts, which static_cast will of course not allow you to do.

However, static_cast's "friends" let you do such stuff, so if you want to shoot your foot, you can do it either way, and if you don't know what you're doing, you'll go to hell either way, too. :lol:

--- End quote ---

Cast is ugly and C-Style cast is uglier, even if easier to use :). C++ Cast Style is better, but this does not mean that it should be used everywhere and everytime :). There are some posts where we have discussed cast related problems. May be you might would like to have a look at here:


* http://forums.codeblocks.org/index.php?topic=1592.0
* http://forums.codeblocks.org/index.php?topic=2259.0
Best wishes,
Michael

thomas:
These two lines taken from a recent patch (the anonymous author will hopefully forgive me posting them here) show that C++ casts are not better in any way:

--- Code: ----    if(abs(compilerIdx)>=CompilerFactory::Compilers.GetCount())
+    if(static_cast<unsigned>(compilerIdx)>=CompilerFactory::Compilers.GetCount())

--- End code ---
This patch's purpose is to remove "comparing signed vs. unsigned " compiler warning because abs declares as int abs(int).

The introduced C++ cast will remove the warning by making the compiler treat the number as unsigned, however, it did not only fool the compiler, but also the programmer!

static_cast<unsigned> looks so good and so innocent. Except that without abs(), it is not unsigned at all, you only treat it as such. Thus, a value of -1 now becomes 4294967295. By mere coincidence (because of the >= comparison) the above code might still work, but it is clearly wrong.

And this is my whole point: C++ casts are not better than C casts.
They are different, more explicit, but not better.

Casts are a means to break the rules of the language and change the semantics of data, not more, not less. Using casts can likewise shoot your foot or blow away your whole leg, whether you use C or C++.  8)

killerbot:
or as Meyers put it, the c++ ones are even uglier and harder to type, so you would think again and try to avoid the need for a cast all together. ;-)
and on the plus side : they are easier to find.

Michael:

--- Quote from: thomas on February 07, 2006, 04:15:12 pm ---These two lines taken from a recent patch (the anonymous author will hopefully forgive me posting them here) show that C++ casts are not better in any way:

--- Code: ----    if(abs(compilerIdx)>=CompilerFactory::Compilers.GetCount())
+    if(static_cast<unsigned>(compilerIdx)>=CompilerFactory::Compilers.GetCount())

--- End code ---
This patch's purpose is to remove "comparing signed vs. unsigned " compiler warning because abs declares as int abs(int).

The introduced C++ cast will remove the warning by making the compiler treat the number as unsigned, however, it did not only fool the compiler, but also the programmer!

static_cast<unsigned> looks so good and so innocent. Except that without abs(), it is not unsigned at all, you only treat it as such. Thus, a value of -1 now becomes 4294967295. By mere coincidence (because of the >= comparison) the above code might still work, but it is clearly wrong.

And this is my whole point: C++ casts are not better than C casts.
They are different, more explicit, but not better.

Casts are a means to break the rules of the language and change the semantics of data, not more, not less. Using casts can likewise shoot your foot or blow away your whole leg, whether you use C or C++.  8)

--- End quote ---

The anonymous author agrees with most of what you have said :D.

IMHO the compiler warning "comparing signed vs. unsigned" should be removed (soon or later :)), because mixing signed and unsigned int could lead to errors difficult to spot. The cast seems worste than the abs() function (because of negative numbers), but this function seems not the final solution too.

I have found some interesting information in these slides.

Michael

[EDIT]: I did last night some tests with cast, abs() and so on in order to understand better and effectively cast unsigned from signed is bad, especially when signed can have a negative value. If the signed will have only positive values, then there are no problems (probably :)), but if it can get negative values then this leads to problems and in some cases (if not in the most :D) to big problems. abs() seems to be here much better solution. The anonymous author has learnt something useful :D.

Navigation

[0] Message Index

[*] Previous page

Go to full version