home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13137 < prev    next >
Encoding:
Text File  |  1992-09-01  |  3.4 KB  |  142 lines

  1. Path: sparky!uunet!mcsun!sun4nl!sara5!hasara11!v80ugeko
  2. Newsgroups: comp.lang.c++
  3. Subject: Borland c++ 3.0 templates and Lipmann`s book
  4. Message-ID: <92245.104942V80UGEKO@HASARA11.BITNET>
  5. From: <V80UGEKO@HASARA11.BITNET>
  6. Date: Tuesday, 1 Sep 1992 10:49:42 CET
  7. Organization: SARA - Stichting Academisch Rekencentrum Amsterdam
  8. Lines: 130
  9.  
  10. // Borland C++ 3.0 templates and Lippman's book
  11. /* I defined a template class "seq" which is a sequence of elements, like
  12.    a C++ vector.
  13.    The elements can be matrices or space vectors or doubles.
  14.  
  15.    The operators +-*/ are overloaded so that the operation is performed on
  16.    an element by element basis. ( For + and - this is straightforward to do
  17.    with template functions, for * most operations must be specific
  18.    instantiated functions , because of the combination operand types &
  19.    return type)
  20. BUT
  21.    For / I only need division by double. This could in my opinion be
  22.    done by a template function:
  23.    friend seq<T> operator/ (const seq<T> &, const seq<double> & );
  24.    but than a problem arises: operator/ must be a friend of both
  25.    an instantiation of the seq<T> class and of seq<double>
  26. */
  27. // Example of a problem with templates.  In the 2nd edition of the C++ primer
  28. // by Lippman, on page 378, a solution is given (I think),
  29. // but that doesn't work.
  30. // Lippman calls it an unbound template class function.
  31.  
  32. #include <iostream.h>
  33. #include <mem.h>
  34. #define error()
  35.  
  36. // some dummy class to make the example possible;
  37. // Most important is the operator/
  38.  
  39. class cls {
  40. public:
  41.  double d;
  42.  cls() {};
  43.  ~cls(){};
  44.  cls(double ini) { d = ini; };
  45.  friend double operator/ (cls c, double d) {
  46.   return (c.d/d);
  47.  }
  48. };
  49.  
  50. // With this template class I want to be able to perform computations om
  51. // sequences of some type, e.g. "cls" or "double"
  52.  
  53. template <class T> class seq {
  54.  
  55.  T *v;
  56.  int s;
  57.  
  58. public:
  59.  seq(int);                                // constructor
  60.  seq(const seq<T>&);                      // copy constructor
  61.  seq<T>& operator=(const seq<T>&);        // assignment operator
  62.  ~seq();                                  // destructor
  63.  
  64.  friend seq<T> operator/ ( const seq<T> &, const seq<double> &);
  65.  
  66.  
  67. };
  68.  
  69.  // template function definitions:
  70.  
  71.  // constructor
  72.  template <class T>
  73.  seq<T>::seq(int sz) {
  74.   v = new T [s = sz];
  75.  }
  76.  
  77.  // copy constructor
  78.  template <class T>
  79.  seq<T>::seq(const seq<T>& src) {
  80.   v = new T [s = src.s];
  81.   memcpy(v,src.v,s*sizeof(T));
  82.  }
  83.  
  84.  // assignment operator
  85.  template <class T>
  86.  seq<T>& seq<T>::operator=(const seq<T>& src) {
  87.   if (this != &src) {
  88.    delete[] v;
  89.    v = new T[src.s];
  90.    memcpy(v,src.v,s*sizeof(T));
  91.   }
  92.   return *this;
  93.  }
  94.  
  95.  
  96.  // destructor:
  97.  template <class T>
  98.  seq<T>::~seq() {
  99.   delete[] v;
  100.  }
  101.  
  102.  
  103.  //template <class T> <== ( Lippman)   doesn't compile.
  104.  // operator seq<T> / seq<double>     // How to make friend of both ??
  105.  // the seq<double> is not accessible from this function !
  106.  template <class T>
  107.  seq<T> operator/ ( const seq<T> &a, const seq<double> &b) {
  108.  if ( a.s != b.s) {
  109.   error();
  110.  }
  111.  T * p1 = a.v;
  112.  double * p2 = b.v;
  113.  seq<T> t(a.s);
  114.  int sz = a.s;
  115.   T *dest = t.v;
  116.   for ( int i = 0 ; i < sz ; i++) {
  117.    *dest++ = *p1++ / *p2++;
  118.   }
  119.   return t;
  120.  }
  121.  
  122.  
  123. // Main program of the example. Silly.
  124.  
  125. const int num = 10;
  126. main()
  127. {
  128.  seq<cls> a(num);
  129.  seq<double> b(num);
  130.  int i;
  131.  for (i=1;i<=num;i++) {
  132.  a(i) = cls(i);
  133.  b(i) = 3;
  134.  }
  135.  a = a/b;                  // Execute the operator/
  136.  for (i=1;i<=num;i++) {
  137.  cout << a(i).d << '\n';
  138.  }
  139.  return (0);
  140. }
  141. // G. Kok VU Amsterdam; email: ifb@sara.nl
  142.