home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1947 < prev    next >
Encoding:
Text File  |  1993-01-01  |  2.0 KB  |  74 lines

  1. Newsgroups: comp.std.c++
  2. Path: sparky!uunet!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Type System
  5. Message-ID: <1993Jan1.172044.9659@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. Date: Fri, 1 Jan 1993 17:20:44 GMT
  10. Lines: 62
  11.  
  12. Further to a previous post describing modifications to the type
  13. system, I want to try to explain the underlying concept with some
  14. further examples.
  15.  
  16. The basic idea is that 'int' and 'T' are object types,
  17. whereas 'int&' or 'const int&' are not object types.
  18. I would say they are 'accessors' to an object of type 'int'.
  19.  
  20. All identifiers (and expressions) are accessors.
  21. Consider:
  22.  
  23.     int x;
  24.     int& y=x;
  25.     const int& z=x;
  26.  
  27. In use, one cannot distinguish 'x' and 'y'. It is absurd IMHO
  28. to pretend that 'x' is of type 'int' whereas 'y' is of type 'int&'.
  29. Instead, one says that 'x' and 'y' and indeed 'z' refer to,
  30. or access, an object of type int.
  31.  
  32. The 'access' method of both 'x' and 'y' is reference to int,
  33. i.e. they are both 'int&'. 'z' is also a reference to an
  34. int, but it has a restriction imposed preventing the call
  35. of non-const functions.
  36.  
  37. Consequences: of this and the previous post:
  38.  
  39. consider a template function:
  40.  
  41. template<class T> f(T){ ... }
  42.  
  43. what function is generated  by the calls
  44.  
  45.     f(3);
  46.     f(x);
  47.     f(y);
  48.     f(z);
  49. ??
  50.  
  51. My system is simple: in all cases
  52.  
  53.     f(int);
  54.  
  55. is used. If you wanted to create
  56.  
  57.     f(int&);
  58.  
  59. you should have defined 
  60.  
  61. template<class T> f(T&) { ...}
  62.  
  63. (in which case the calls f(3), f(z) are illegal)
  64.  
  65. The reason is trivial: <class T> stands for a class (or pointer)
  66. type, ie an object type, and int& is not a type, but a reference
  67. to an object of the type int.
  68.  
  69. -- 
  70. ;----------------------------------------------------------------------
  71.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  72.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  73. ;--------------- SCIENTIFIC AND ENGINEERING SOFTWARE ------------------
  74.