home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / intkyset / demoelem.h next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  2.9 KB  |  96 lines

  1. /**********************************************************************
  2. *                                                                     *
  3. *  IBM(R) VisualAge(TM) for C++ for Windows(R), Version 3.5           *
  4. *                                                                     *
  5. *  PID: 5622-880                                                      *
  6. *  - Licensed Material - Program-Property of IBM                      *
  7. *  (C) Copyright IBM Corp. 1991, 1995 - All Right Reserved.           *
  8. *                                                                     *
  9. *  US Government Users Restricted Rights - Use, duplication or        *
  10. *  disclosure restricted by GSA ADP Schedule Contract with IBM Corp.  *
  11. *                                                                     *
  12. *  VisualAge, and IBM are trademarks or registered trademarks of      *
  13. *  International Business Machines Corporation.                       *
  14. *  Windows is a registered trademark of Microsoft Corporation.        *
  15. *                                                                     *
  16. **********************************************************************/
  17.  
  18. /*-------------------------------------------------------------*\
  19. |  demoelem.h  -  DemoElement for use with Key Collections      |
  20. \*-------------------------------------------------------------*/
  21. #ifndef _DEMOELEM_H
  22. #define _DEMOELEM_H
  23.  
  24. #include <stdlib.h>
  25. #include <iglobals.h>
  26. #include <iostream.h>
  27. #include <istdops.h>
  28.  
  29. class DemoElement {
  30.  
  31.   int i;
  32.   int j;
  33.  
  34. public:
  35.  
  36.            DemoElement ()
  37.            : i(0), j(0)
  38.            {
  39.            }
  40.  
  41.            DemoElement (int i,int j)
  42.            : i (i), j(j)
  43.            {
  44.            }
  45.  
  46.            operator int () const
  47.            { return i;
  48.            }
  49.  
  50.   IBoolean operator == (DemoElement const& k) const
  51.            { return i == k.i && j == k.j;
  52.            }
  53.  
  54.   IBoolean operator < (DemoElement const& k) const
  55.            { return i < k.i || (i == k.i && j < k.j);
  56.            }
  57.  
  58.   friend unsigned long
  59.            hash (DemoElement const& k, unsigned long n)
  60.            { return k.i % n;
  61.            }
  62.  
  63.   int const &
  64.            geti () const
  65.            { return i;
  66.            }
  67.  
  68.   int const &
  69.            getj () const
  70.            { return j;
  71.            }
  72.  
  73. };
  74.  
  75. inline ostream & operator << (ostream &sout, DemoElement const& e)
  76. { sout << e.geti () << "," << e.getj ();
  77.   return sout;
  78. }
  79.  
  80. inline int const& key (DemoElement const& k)
  81. { return k.geti ();
  82. }
  83.  
  84. // NOTE: You must return a const & in the key function!
  85. // Otherwise the implicitly created standard element operations
  86. // will return a reference to a temporary. This would lead to
  87. // incorrect behavior of the collection operations.
  88.  
  89. // The key function must be declared in the header file of
  90. // the collection's element type.
  91.  
  92. // If either of these is not possible or undesirable,
  93. // an element operations class must be used.
  94.  
  95. #endif
  96.