Instead of "old" C style casts:
Prefer C++ style-casts:
Simply use
Note: Minimize use of casts, specially dynamic_cast.
prev | next
(T) expression // cast expression to be of type T T(expression) // cast expression to be of type T
Prefer C++ style-casts:
- const_cast: To convert const datatype to non-const datatype.
- reinterpret_cast: For low-level cast.
- static_cast: to force implicit casts.
- dynamic_cast: to perform safe-downcasting. For example:
Derived *pd = new Derived(); if ( Base *pb = dynamic_cast < Base* > pd ) doSomething(); // returns true; if ( SuperDerived *pd2 = dynamic_cast <SuperDerived*> pd ) doSomething(); // returns false;
class Window { // base class public: virtual void onResize() { ... } // base onResize impl ... }; class SpecialWindow: public Window { // derived class virtual void onResize(){ // derived onResize impl; static_cast<Window>(*this).onResize(); // cast *this to Window, // then call its onResize; // this doesn't work! ... // do SpecialWindow- } // specific stuff ... };
Simply use
Window::onResize();instead of the line 9 above.
Note: Minimize use of casts, specially dynamic_cast.
prev | next
No comments:
Post a Comment