Linked Questions
244 questions linked to/from When should static_cast, dynamic_cast, const_cast, and reinterpret_cast be used?
2065 votes
8 answers
961k views
Regular cast vs. static_cast vs. dynamic_cast [duplicate]
I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e. MyClass *m = (MyClass *)...
307 votes
7 answers
186k views
What is the difference between static_cast<> and C style casting? [duplicate]
Is there any reason to prefer static_cast<> over C style casting? Are they equivalent? Is there any sort of speed difference?
72 votes
1 answer
68k views
What is the difference between static_cast and reinterpret_cast? [duplicate]
Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? I'm using c function in c++, where a structure passed as a void type argument in c is directly stored that ...
51 votes
2 answers
76k views
"warning: use of old-style cast" in g++ [duplicate]
Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? With this C++ code, char* a = (char*) b; I got warning warning: use of old-style cast. What would be the ...
11 votes
4 answers
27k views
C++ Polymorphism: from parent class to child [duplicate]
In C++ we can convert child class pointer to parent, but is there any way to convert it back: from parent, which was obtained from child, give child class back? I mean: class Parent { ... }; ...
27 votes
2 answers
35k views
Best practice in C++ for casting between number types [duplicate]
What is the best practice for casting between the different number types? Types float, double, int are the ones I use the most in C++. An example of the options where f is a float and n is a double ...
8 votes
3 answers
3k views
C++ Casting Operators and traditional C casting operators [duplicate]
Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? I have done a lot of googling to find about: why to use C++ casting operators over traditional C style ...
6 votes
4 answers
8k views
C++ type casting [duplicate]
Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? Until a few days ago, I've always used C style type casting in C++ because it seemed to work good. I ...
2 votes
1 answer
1k views
Difference between functional cast notation T(x) and static_cast<T>(x) [duplicate]
(I know this is very similar to some other questions on here, but I haven't found any that specifically, language-lawyerly, answer this precise detail. Most of the near-duplicates are just asking ...
4 votes
2 answers
605 views
Dynamic_cast not needing to perform a run-time check? [duplicate]
Quoting from item 45 in C++ Gotchas: First a dynamic_cast is not necessarily dynamic, in that it may not perform a runtime check. When performing a dynamic_cast from a derived class pointer (or ...
0 votes
1 answer
2k views
c++ - Why a std::vector<>::const_reference can cast to a non-const pointer? [duplicate]
In g++ , element access operator for const std::vector<> is defined as follows: (/usr/include/c++/7.1.1/bits/stl_vector.h) /** * @brief Subscript access to the data contained in the %vector. ...
-2 votes
3 answers
1k views
What is the benefit/use of dynamic_cast? [duplicate]
class Base { public: virtual void print (){cout<<"Base"<<endl;} }; class Derived :public Base { public: virtual void print (){cout<<"Derived"<<...
0 votes
2 answers
214 views
Can reinterpret_cast be considered as an unsafe workaround when dynamic_cast is unavailable? [duplicate]
I am using C++ to write some components in a toy x86 kernel, and I was in a situation where I had to downcast a pointer type. I had an interface and its implementation like this, class ITerminalWriter ...
4 votes
2 answers
861 views
What is the purpose of reinterpret_cast [duplicate]
I'm new to C++ and reading some code as follows: template<typename T> std::istream & read(std::istream* stream, T& value){ return stream->read(reinterpret_cast<char*>(&...
-2 votes
1 answer
682 views
Use static_cast to dynamic polymorphism [duplicate]
I used a static_cast to polymorphism. I read that I should use dynamic_cast, but I can't get why. Code works in both way (with static and dynamic cast). Could anybody show me an example with ...