home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / tctnt / static.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  1.1 KB  |  37 lines

  1. /* STATIC.CPP: Initializing Static Class Members of Type Class
  2.  
  3.          The C++ specification requires that static members of a class
  4.          be initialized and that this initialization be done outside of
  5.          the class and at global level.  Failure to do this will inevitably
  6.          result in an "undefined symbol" linker error.  The following code
  7.          illustrates the static member initialization required.
  8.  
  9.          Note that you must declare an instance of the class in a main
  10.          function before TLINK will find it necessary to generate an
  11.          "undefined symbol" error.
  12. */
  13.  
  14. // declare a class
  15. class First {
  16.     public:
  17.         int count;
  18.         First() {}  //must have callable constructor
  19. };
  20.  
  21. //declare class containing static member of class First
  22. class Second {
  23.     public:
  24.         static First fVar;
  25. };
  26.  
  27. //initialize static member of class Second
  28. First Second::fVar = First();  //initialize with explicit call to ctor
  29.  
  30. //*******************************************************************
  31. int main(void)
  32. {
  33.     Second *sVarA = new Second;
  34.     delete sVarA;
  35.     return 0;
  36. } // end of main()
  37.