home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18118 < prev    next >
Encoding:
Text File  |  1992-12-17  |  2.0 KB  |  66 lines

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!agate!doc.ic.ac.uk!sot-ecs!tpm
  2. From: tpm@ecs (TP Monks)
  3. Newsgroups: comp.lang.c++
  4. Subject: ambiguity in subclass
  5. Message-ID: <13997@ecs.soton.ac.uk>
  6. Date: 17 Dec 92 11:21:16 GMT
  7. Sender: news@ecs.soton.ac.uk
  8. Lines: 55
  9. Nntp-Posting-Host: elstar
  10.  
  11.  
  12. I've run into a problem where I'm not quite sure how to implement something.
  13.  
  14. I wanted to implement a set of classes using templates which have a
  15. set of member functions like this:
  16.  
  17. template<class T> struct bill {
  18.     bill() {}
  19.     T& operator*(T);
  20.     T& operator+(T);
  21.     T& operator-(T);
  22.     T& operator/(T);
  23. }
  24.  
  25. These are used to implement some arithmetic and other operations on 
  26. small arrays.  I wish to create a well known set of subclasses which
  27. further overload these operations:
  28.  
  29. struct ted : public bill<float> {    
  30.     ted() {}
  31.     ted& operator*(bogus);
  32. }
  33. struct bogus : public bill<short> {    
  34.     bogus() {}
  35.     bogus& operator*(ted);
  36. }
  37.  
  38. Of course, this can't be done because, because of ambiguity:
  39. ted t;
  40. ted t1=t*.1;        // error, operator*(float) is hidden
  41.           
  42. I could get round it by using:
  43. ted t1=t.bill<float>::operator*(.1);    // ok
  44. But lets face it, its just not the same.             
  45.  
  46. I could also get round it by adding another member fn to bill and ted
  47. along the lines of:
  48. inline ted& ted::operator*(float a) { return bill<float>::operator*(a);}
  49. I could go further and implement *,+,-,etc using macros but surely this
  50. is clumsy and must be unecessary.
  51.  
  52. Please tell me if there is something fundamentally wrong with my thinking.
  53. (Please *don't* tell me if I forgot a ; or something!!!)
  54. If there isn't, surely what I am doing is a common problem.  In this
  55. case, what is the best way to solve this problem and implement my classes?
  56.  
  57. Help!
  58.  
  59. Tim
  60.  
  61. --
  62. -----------------------------------------------------------------------------
  63. Tim Monks                                Vision, Speech and Signal Processing
  64. tpm@ecs.soton.ac.uk                                    Southampton University
  65. Tel: +44 (703) 592774                                   Fax: +44 (703) 592895
  66.