前向声明的几种方式
前向声明
class A:
A f();
详细类型声明
class A f();
两种方式的区别
-
前向声明
struct s { int a; }; struct s; // does nothing (s already defined in this scope) void g() { struct s; // forward declaration of a new, local struct "s" // this hides global struct s until the end of this block s* p; // pointer to local struct s struct s { char* p; }; // definitions of the local struct s }
-
详细类型说明
class U; namespace ns{ class Y f(class T p); // declares function ns::f and declares ns::T and ns::Y class U f(); // U refers to ::U Y* p; T* q; // can use pointers and references to T and Y }
详细类型说明,只有在查找不到对应名字的同类型的东西,才会引入前向声明的效果。