当需要在一个类中用到信赖自身的类定义时,需要用到前向声名,比如下面的例子,注意第1-3行,定义的就是IdlInterface的类,在该类本身就需要引用到InterfaceList但InterfaceList又信赖于IdlInterface,这种定义方法就是前向声名。
1 class IdlInterface; 2 typedef std::list<IdlInterface*> InterfaceList; 3 typedef std::list<IdlInterface*>::iterator InterfaceIterator; 4 5 class IdlInterface : public IdlType { 6 7 private: 8 /* * All functions */ 9 FunctionList __functions; 10 /* * Old type's instance */ 11 InterfaceList __interfaces; 12 13 public: 14 /* * Adds a new function to interface */ 15 void addFunction(IdlFunction*); 16 /* * Gets a pointer to current functions list */ 17 FunctionList* getFunctions(); 18 19 /* * Adds a new interface */ 20 void addInterface(IdlInterface*); 21 /* * Gets the interface reference */ 22 InterfaceList* getInterfaces(); 23 24 /* * Casts to std::string for convenience of printing */ 25 std:: string toString(); 26 };