static-cast

Why is it important to use static_cast instead of reinterpret_cast here?

时间秒杀一切 提交于 2019-11-30 19:24:49
At a reply of a blog post of Raymond Chen , A questioner pointed out Raymond, I believe the C++ example is not correct since the position of the base class subobject in the derived class is unspecified according to ISO C++ 2003 Standard (10-3, page 168), and you assume that the base class subobject is always at the beginning. The C example would be fine in C++ too, so I'd stick with it. Raymond replied [The code does not make this assumption. That's why it's important to use static_cast instead of reinterpret_cast. Try it: Add a virtual method to OVERLAPPED (so a vtable goes in front) and

Safety of invalid downcast using static_cast (or reinterpret_cast) for inheritance without added members

為{幸葍}努か 提交于 2019-11-30 18:55:14
问题 I was wondering what the standard says about the safety of the following code: class A { int v; }; class B: public A { }; // no added data member A a; B& b = static_cast<B&>(a); Obviously the runtime type of a is A , not B , so the cast is not really type safe. However, since there was no member added and nothing is virtual, IMO the memory layout of the classes should be the same and this should work (maybe it would be nicer to write reinterpret_cast to indicate this behaviour?). My guess

Union vs. static_cast(void*)

丶灬走出姿态 提交于 2019-11-30 09:31:25
问题 I'm writing code and until now I was using structures like this: struct s{ enum Types { zero = 0, one, two }; unsigned int type; void* data; } I needed some generic structure to store data from different classes and I wanted to use it in std::vector, so that's reason why I can't use templates. What's better option: unions or void pointers? Void pointer allocates only as much space as I need, but c++ is strong typed language for some reason and casting everywhere I need to use those data is

C++: can't static_cast from double* to int*

此生再无相见时 提交于 2019-11-30 07:54:59
问题 When I try to use a static_cast to cast a double* to an int*, I get the following error: invalid static_cast from type ‘double*’ to type ‘int*’ Here is the code: #include <iostream> int main() { double* p = new double(2); int* r; r=static_cast<int*>(p); std::cout << *r << std::endl; } I understand that there would be problems converting between a double and an int, but why is there a problem converting between a double* and an int*? 回答1: Aside from being pointers, double* and int* have

Union vs. static_cast(void*)

99封情书 提交于 2019-11-29 16:18:16
I'm writing code and until now I was using structures like this: struct s{ enum Types { zero = 0, one, two }; unsigned int type; void* data; } I needed some generic structure to store data from different classes and I wanted to use it in std::vector, so that's reason why I can't use templates. What's better option: unions or void pointers? Void pointer allocates only as much space as I need, but c++ is strong typed language for some reason and casting everywhere I need to use those data is not the way c++ code should be designed. As I read, void pointers shouldn't be used unless there's no

C++: can't static_cast from double* to int*

懵懂的女人 提交于 2019-11-29 05:37:52
When I try to use a static_cast to cast a double* to an int*, I get the following error: invalid static_cast from type ‘double*’ to type ‘int*’ Here is the code: #include <iostream> int main() { double* p = new double(2); int* r; r=static_cast<int*>(p); std::cout << *r << std::endl; } I understand that there would be problems converting between a double and an int, but why is there a problem converting between a double* and an int*? Aside from being pointers, double* and int* have nothing in common. You could say the same thing for Foo* and Bar* pointer types to any dissimilar structures.

static_cast with boost::shared_ptr?

心已入冬 提交于 2019-11-28 14:55:37
问题 What is the equivalent of a static_cast with boost::shared_ptr ? In other words, how do I have to rewrite the following Base* b = new Derived(); Derived* d = static_cast<Derived*>(b); when using shared_ptr ? boost::shared_ptr<Base> b(new Derived()); boost::shared_ptr<Derived> d = ??? 回答1: Use boost::static_pointer_cast : boost::shared_ptr<Base> b(new Derived()); boost::shared_ptr<Derived> d = boost::static_pointer_cast<Derived>(b); 回答2: There are three cast operators for smart pointers:

What does static_cast<T> do to a T&?

旧巷老猫 提交于 2019-11-28 11:22:34
So I asked this question and I was tinkering around with solving it via static_cast . (Incidentally it does solve the problem, I'm just not sure if I understand why.) In the code: vector<int> foo = {0, 42, 0, 42, 0, 42}; replace(begin(foo), end(foo), static_cast<int>(foo.front()), 13); Is the static_cast simply constructing an R-Value int ? What's the difference between that and just the call: replace(begin(foo), end(foo), int{foo.front()}, 13); EDIT: As inferred by the answers static_cast does seem to construct an R-Value int : http://ideone.com/dVPIhD But this code does not work on Visual

Why can't I static_cast between char * and unsigned char *?

烈酒焚心 提交于 2019-11-28 05:42:28
Apparently the compiler considers them to be unrelated types and hence reinterpret_cast is required. Why is this the rule? EdChum They are completely different types see standard: 3.9.1 Fundamental types [basic.fundamental] 1 Objects declared as characters char) shall be large enough to store any member of the implementation's basic character set. If a character from this set is stored in a character object, the integral value of that character object is equal to the value of the single character literal form of that character. It is implementation-defined whether a char object can hold

Proper way of casting pointer types

最后都变了- 提交于 2019-11-28 04:38:47
Considering the following code (and the fact that VirtualAlloc() returns a void* ): BYTE* pbNext = reinterpret_cast<BYTE*>( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast ? I used to think that reinterpret_cast is OK for e.g. casting pointers to and from integer types (like e.g. DWORD_PTR ), but to cast from a void* to a BYTE* , isn't static_cast OK? Are there any (subtle?) differences in this particular case, or are they just both valid pointer casts? Does the C++ standard have a preference for this case, suggesting a way