Advertisement
Guest User

ForwardDeclaration

a guest
Apr 17th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <list>
  2.  
  3. class A;
  4.  
  5. class B
  6. {
  7.     B();
  8. public:
  9.     A* a; //only this use is allowed with only a forward declaration, without the actual declaration
  10.     //A a2; // this would require the declaration of A
  11. };
  12.  
  13.  
  14. //actual declaration of class A:
  15. class A
  16. {
  17. public:
  18.     A();
  19.     int i;
  20.     typedef std::list<int> AiLst;
  21. };
  22.  
  23.  
  24.  
  25. int main()
  26. {
  27.     // using something else than declaring a pointer to A is allowed now.
  28.     A::AiLst lst;
  29.     A a;
  30.     return 0;
  31. }
  32.  
  33. // this is the definition and can come later in the same translation unit.
  34. A::A():i(0){}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement