Forward declaration บอกคอมไพเลอร์ว่ามีคลาสชื่อนี้อยู่ (จะนิยามภายหลัง) ใช้ได้เมื่อ:
- ต้องการใช้ชื่อคลาสก่อน definition (circular dependencies)
- ลด header dependencies (compile time) - forward declare ใน header, include จริงใน .cpp
- Pimpl idiom (Pointer to Implementation)
ตัวอย่าง
class Person; // Forward declaration
void myFunc(Person p1) { ... } // ใช้ได้ แม้ยังไม่เห็น definition
class Person { // Definition จริง
// ...
};
ข้อจำกัด: ไม่สามารถใช้ขนาด (sizeof), สร้าง object, เข้าถึง members ได้ จนกว่าจะเห็น full definition