User forums > General (but related to Code::Blocks)
Compile error
sethjackson:
@polygon7
Why the
--- Code: (cpp) ---<< 2
--- End code ---
instead of
--- Code: (cpp) --- * 4
--- End code ---
?
This line
--- Code: (cpp) ---unsigned long imageSize = (image.GetWidth() * image.GetHeight()) << 2;
--- End code ---
@Michael How to use a C++ style cast in this case??? Yes I know static_cast, dynamic_cast etc. How to use it here???
polygon7:
--- Quote from: sethjackson on February 05, 2006, 10:55:55 pm ---@polygon7
Why the
--- Code: (cpp) ---<< 2
--- End code ---
instead of
--- Code: (cpp) --- * 4
--- End code ---
?
This line
--- Code: (cpp) ---unsigned long imageSize = (image.GetWidth() * image.GetHeight()) << 2;
--- End code ---
--- End quote ---
"<<" is shifting bits operator (shift left, SHL in assembly). SHL can be used for multiply by power of two (SHR, shift right for dividing by power of two).
x*2 == x <<1, x*4 == x <<2 ...
Bit shifting usually is faster then normal multiply.
Michael:
--- Quote from: sethjackson on February 05, 2006, 10:55:55 pm ---@Michael How to use a C++ style cast in this case??? Yes I know static_cast, dynamic_cast etc. How to use it here???
--- End quote ---
In your case and if I am not wrong, I would use the static_cast Operator:
--- Quote ---static_cast<T> (expr)
To convert the expression expr to type T. Such conversions rely on static (compile-time) type information.
--- End quote ---
Anyway, I am not an expert in C++ cast as usually I used C cast until I discovered that are not so good (cast is already ugly, but C-cast is uglier). You can get some useful info here:
* http://www.research.att.com/~bs/bs_faq2.html#static-cast
* http://en.wikipedia.org/wiki/Cast_(computer_science)#in_C.2FC.2B.2B
* http://www.coding-zone.co.uk/cpp/articles/050101casting.shtml
You can also have a look at this topic.
Michael
sethjackson:
Ok guys thanks. I learnt something today. :)
@Michael yeah I always use C++ casts instead of C casts. I couldn't figure out how to get it to work here. What type do I need to static_cast to?? I tried static_cast, but I couldn't figure out which type to cast to. :P
Michael:
--- Quote from: sethjackson on February 06, 2006, 12:01:07 am ---@Michael yeah I always use C++ casts instead of C casts. I couldn't figure out how to get it to work here. What type do I need to static_cast to?? I tried static_cast, but I couldn't figure out which type to cast to. :P
--- End quote ---
For example, you can use it to cast a double to an int:
--- Code: ---double myDouble = 3.0;
int myInt = static_cast<int>(myDouble);
--- End code ---
Anyway, be careful because static casts could be dangerous.
Have a look at the links I have suggested. There is a lot of examples and useful bla bla :).
Michael
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version