Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

ForwardDeclaration

By: a guest on Apr 17th, 2013  |  syntax: C++  |  size: 0.54 KB  |  views: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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){}