Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 25th, 2012  |  syntax: None  |  size: 1.11 KB  |  hits: 14  |  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. Outer class containing inner class - instantiate the inner class as a member of the outer class
  2. #ifndef TERMINALLOG_HH
  3. #define TERMINALLOG_HH
  4.  
  5. #include <string>
  6. #include <sstream>
  7. #include <iostream>
  8.  
  9. class Terminallog {
  10. public:
  11.  
  12.     Terminallog();
  13.     virtual ~Terminallog();    
  14.  
  15.     class Warn {
  16.     public:
  17.         Warn();
  18.  
  19.     private:
  20.         bool lineended;  
  21.     };
  22.     friend class Terminallog::Warn;
  23.  
  24. protected:
  25.  
  26. private:
  27.     Warn warn;    
  28.  
  29. };
  30.        
  31. // stripped code
  32.  
  33.  Terminallog::Terminallog() {
  34.      Warn this->warn();
  35.  }
  36.  
  37. Terminallog::Warn::Warn() {
  38.     this->lineended = true;
  39. }
  40.  
  41. //stripped code
  42.        
  43. g++ src/terminallog.cc inc/terminallog.hh -o test -Wall -Werror
  44. In file included from src/terminallog.cc:8:
  45. src/../inc/terminallog.hh:56: error: declaration of ‘Terminallog::Warn Terminallog::warn’
  46. src/../inc/terminallog.hh:24: error: conflicts with previous declaration ‘void Terminallog::warn(std::string)’
  47.        
  48. Terminallog::Terminallog() : warn()
  49. {
  50.    // other constructor code
  51. }
  52.  
  53.  
  54. // note that `Warn::Warn()` is invoked implicitly on `wake`, so
  55.  
  56. TerminalLog::Terminallog() {}
  57.  
  58. // would be equivalent