home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / cplus / 11506 < prev    next >
Encoding:
Text File  |  1992-07-25  |  1.9 KB  |  55 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!ftpbox!mothost!lmpsbbs!supra.comm.mot.com!rittle
  3. From: rittle@supra.comm.mot.com (Loren James Rittle)
  4. Subject: Use of constructor within class declaration
  5. Organization: Land Mobile Products Sector, Motorola Inc.
  6. Date: Fri, 24 Jul 1992 23:29:26 GMT
  7. Message-ID: <1992Jul24.232926.18488@lmpsbbs.comm.mot.com>
  8. Sender: rittle@comm.mot.com (Loren J. Rittle)
  9. Nntp-Posting-Host: 145.1.80.40
  10. Lines: 43
  11.  
  12. Hello net friends,
  13.  
  14. Can anyone explain what is happening below?  Why can I not use a class'
  15. constructor as a default argument in a function defined in a class?  Is
  16. there a good way around this problem (other than moving the function
  17. definition out of the class OR using overloading)?  Does any version of
  18. AT&T C++ fix this problem (I can find no reference in the 'BS' C++
  19. manual about this.  I'm just learning C++ so I may have missed something)?
  20.  
  21. A person I work with claims that even if I could use a constructor as I
  22. wish, it would be bad style.  What is your opinion?  To me it is as
  23. natural to want to use a constructor anywhere in the class  declaration
  24. after it is declared as it is to refer to a class' members later within
  25. the class declaration.
  26.  
  27. The example has been kept short in the hopes that someone will take the
  28. time to respond.  Thanks for email or posted responses that can be
  29. summerized upon demand.
  30.  
  31. Regards,
  32. Loren
  33.  
  34. $ gcc test2.C  # gcc 2.2.2
  35. test2.C:7: type `point' is not yet defined
  36. $ cxx test2.C  # DEC cxx 2.1
  37. test2.C:7: error: In this declaration, "point" is an incomplete or
  38. function type and so cannot be the target type of a conversion.
  39. Compilation terminated with errors.
  40.  
  41. - --- begin test2.C ---
  42. class point
  43. {
  44.   double _x, _y;
  45. public:
  46.   point (double x = 0.0, double y = 0.0) : _x(x), _y(y) {}
  47. #if 1
  48.   double distance (point o = point (0.0, 0.0)) { return /* ... */ 0.0; }
  49. #else
  50.   double distance (point o) { return /* ... */ 0.0; }
  51.   double distance (void) { return /* ... */ 0.0; }
  52. #endif
  53. };
  54. - --- end test2.C ---
  55.