home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / cplus / 18171 < prev    next >
Encoding:
Internet Message Format  |  1992-12-20  |  2.1 KB

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