05 C++ My Notes
When to use dynamic_cast?
Use dynamic_cast for polymorphic downcasting that would check type correctness at runtime with performance overhead, and return nullptr of type of castee is not compatible for the target type for pointer castee, or an bad_cast exception will be thrown for reference castee.
When to use static_cast?
Use it whenever possible to perform static checking by compiler to make sure type is compatible. Use it for upcast. Valid but dangerous to perform static downcast because may generate undefined behavior.
When to use reinterpret_cast?
It is the same as C style cast and should seldom be used.
When to use const_cast?
Scott Meyers said a good use of const_cast is to avoid code duplication by implementing the non-const member function in terms of const member function by first static casting the object itself as a const object to call the const member function, and then const cast away the const return type to non const type, i.e. T& get() {return const_cast<T&>(static_cast<const C&>(*this).get());
2017年2月24日 星期五
訂閱:
張貼留言 (Atom)
202501 Debian USB LAN Card Bridge
202501 Debian USB LAN Card Bridge ChatGPT Question I have a machine running debian 12 with a LAN port using a cable to connect to my office...
-
On CentOS 7, using yum install gives you cmake version 2.8.11 [root@thrift1 ~]# cat /etc/*release CentOS Linux release 7.2.1511 (Core) ...
-
Synology DSM 5.2 on Virtual Box Files On my Google Drive "2016DSM5.2" or download link below (3 files total: pat, iso, exe) ...
-
Static ZeroMQ (difficult setup) cd /d C:\ mkdir Repos cd /d C:\Repos\ git clone https://github.com/Microsoft/vcpkg cd /d C:\Repos\v...
沒有留言:
張貼留言