ประเภทของ Cast ใน C++
static_cast
ใช้สำหรับการแปลงชนิดข้อมูลปกติ (ordinary type conversion) ยังรวม implicit type coercion และเรียกได้โดยตรง ใช้ในกรณีเช่น:
- แปลง
float→int,char→int - แปลง pointer ระหว่างคลาสที่เกี่ยวข้อง (base ↔ derived) เมื่อแน่ใจว่าปลอดภัย (ไม่มี runtime check)
- แปลง enum → int, void* → T*
dynamic_cast
ใช้สำหรับจัดการ polymorphism ใช้เฉพาะเมื่อ cast ไปยัง derived class (downcast) ต้องมี RTTI (Run-Time Type Information) และ base class ต้องมี virtual function อย่างน้อย 1 ตัว
- Pointer: คืน
nullptrหาก cast ไม่ได้ (ปลอดภัย) - Reference: โยน
std::bad_castexception หาก cast ไม่ได้ - ใช้เมื่อ ไม่แน่ใจ ว่าออบเจ็กต์เป็นประเภท derived class จริงหรือไม่
Regular Cast (C-style cast)
รูปแบบ (Type)expr หรือ Type(expr) เป็น cast ที่ทรงพลังที่สุด เพราะรวม:
const_cast(เพิ่ม/ลบ const)static_cast(การแปลงปกติ)reinterpret_cast(reinterpret bit pattern)
แต่ unsafe เพราะไม่ใช้ dynamic_cast (ไม่มี runtime check) และอาจทำการแปลงที่อันตรายโดยไม่ได้ตั้งใจ (เช่น cast away const แล้ว modify → UB)
Cast อื่นๆ
const_cast
เพิ่มหรือลบ const/volatile qualifier เหมาะสำหรับ API เก่าที่ไม่ const-correct
reinterpret_cast
Reinterpret bit pattern ระดับต่ำสุด ใช้สำหรับ:
- แปลง pointer ↔ integer
- แปลง pointer type ที่ไม่เกี่ยวข้อง (T* → U*)
- Type punning (อ่าน memory ด้วย type อื่น) - ต้องระมัดระวัง strict aliasing rules
แนวทางปฏิบัติที่ดี (Best Practices)
- หลีกเลี่ยง C-style cast ใช้ named casts แทน
- ใช้
static_castสำหรับการแปลงที่แน่ใจว่าปลอดภัย (compile-time) - ใช้
dynamic_castสำหรับ downcasting ใน polymorphic hierarchy (runtime check) - ใช้
const_castเฉพาะเมื่อจำเป็นจริงๆ (API legacy) - ใช้
reinterpret_castน้อยที่สุด และ comment ให้ชัดเจนว่าทำไม - C++20: ใช้
std::bit_castสำหรับ type punning ที่ปลอดภัย