home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / cplus / 16650 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.3 KB  |  77 lines

  1. Path: sparky!uunet!snorkelwacker.mit.edu!ai-lab!life.ai.mit.edu!tmb
  2. From: tmb@arolla.idiap.ch (Thomas M. Breuel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Template classes and overloaded binary operators on differing types
  5. Date: 20 Nov 92 17:35:58
  6. Organization: IDIAP (Institut Dalle Molle d'Intelligence Artificielle
  7.     Perceptive)
  8. Lines: 62
  9. Message-ID: <TMB.92Nov20173558@arolla.idiap.ch>
  10. References: <1egmvkINNi35@emory.mathcs.emory.edu>
  11. Reply-To: tmb@idiap.ch
  12. NNTP-Posting-Host: arolla.idiap.ch
  13. In-reply-to: sb@next.neuro.emory.edu's message of 19 Nov 1992 18:38:44 GMT
  14.  
  15.      Can someone please comment on the following problem and suggest the
  16.    "best" way to solve it.
  17.  
  18.       Say I have a template class that implements a VECTOR type and binary
  19.     operators like addition and subtraction. All of the binary operators are  
  20.    currently implemented as template friend functions which accept two  
  21.    vectors of the same type. For example, I have this :
  22.  
  23.  
  24.      VECTOR<int> ivec1(3),ivec2(3),ivec3(3);  // 3 is vector size
  25.  
  26.      ivec3 = ivec1 + ivec2;   
  27.  
  28.      My question is, how can I extend this so that I can add vectors of  
  29.    differing types ?  e.g.,
  30.  
  31.        VECTOR<int> ivec1(3);
  32.        VECTOR<double> dvec1(3),dvec2(3);
  33.  
  34.        dvec2 = ivec1 + dvec1;
  35.  
  36.     What is a good way to implement this ?
  37.  
  38. There are several methods:
  39.  
  40.     (1) define a conversion operator:
  41.  
  42.     template <class T>
  43.     struct VECTOR {
  44.         ...
  45.         operator VECTOR<int> { ... }
  46.         operator VECTOR<double> { ... }
  47.         ...
  48.     };
  49.  
  50.     (2) write an explicit conversion function (can be a template
  51.     function for convenience):
  52.  
  53.     template <class T>
  54.     VECTOR<double> as_double_VECTOR(VECTOR<T> &v) {
  55.         ...
  56.     };
  57.  
  58.     (3) overload operator+:
  59.  
  60.     VECTOR<double> operator+(VECTOR<int> &v1,VECTOR<double> &v2) {...}
  61.  
  62.     (4) overload operator+ with a template:
  63.  
  64.     template <class X,class Y>
  65.     VECTOR<X> operator+(VECTOR<X> &v1,VECTOR<Y> &v2) {...}
  66.  
  67. Personally, I'd use (and I have used) (2). Method (1) doesn't work
  68. with GNU C++. Method (3) is usually considered the "most efficient",
  69. but in terms of runtime, the difference is usually insignificant; the
  70. main difference is that it requires less space than conversion
  71. methods, but it requires a whole lot more code. Method (4) may look
  72. appealing, but the return type is wrong (sometimes you want VECTOR<X>,
  73. sometimes VECTOR<Y>), and there is no way of fixing this in C++.
  74.  
  75.                 Thomas.
  76.  
  77.