home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / gnu / g / help / 1414 < prev    next >
Encoding:
Text File  |  1992-11-08  |  2.7 KB  |  85 lines

  1. Newsgroups: gnu.g++.help
  2. Path: sparky!uunet!charon.amdahl.com!pacbell.com!sgiblab!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!news.cs.indiana.edu!babbage.ece.uc.edu!dain
  3. From: dain@holmes.ece.uc.edu (A. Dain Samples)
  4. Subject: `new' again
  5. Message-ID: <1992Nov6.213308.24315@babbage.ece.uc.edu>
  6. Followup-To: gnu.g++.help
  7. Originator: dain@holmes.ece.uc.edu
  8. Sender: root@babbage.ece.uc.edu (Operator)
  9. Nntp-Posting-Host: holmes.ece.uc.edu
  10. Reply-To: Dain.Samples@uc.edu
  11. Organization: University of Cincinnati
  12. Date: Fri, 6 Nov 1992 21:33:08 GMT
  13. Lines: 70
  14.  
  15.  
  16. My students and I are trying to implement classes with customized
  17. `new' operators.  Consider the following code:
  18.  
  19. ================================================================
  20. class A {
  21.   public:
  22.     void * operator new(size_t, int p,uint *p2,uint *p3);
  23. };
  24.  
  25. class B:A{
  26.            int a;
  27.            int b;
  28.   public:
  29.     B();  // **NOTE 1**
  30.     
  31.  
  32. };
  33.  
  34. B::B(){}  // **NOTE 2**
  35.  
  36. main () {
  37.   uint q1, *q2, *q3;
  38.   int i;
  39.  
  40.   q1=sizeof(B);
  41.   q2=(uint *)2;
  42.   q3=(uint *)3;
  43.   
  44.   B *pa=new(q1,q2,q3) B;
  45.   : // etc.
  46. ================================================================
  47.  
  48. When we compile this with g++, we get an error message on the line
  49. marked **Note 2** that says ``too few parameters to new operator''.
  50.  
  51. However, if we remove the lines marked **Note i** -- that is, we
  52. do not declare a constructor for derived class B, then the program
  53. works as expected!  The new operator for base class A is invoked when
  54. we create a B.  
  55.  
  56. 1) Why should a constructor for B interfere with the invocation of the
  57. new defined in base class A?
  58.  
  59. 2) Does the constructors for a derived class intercept calls on new?
  60.  
  61. 3) Have I totally missed the boat here, or does g++ implement a
  62. totally different paradigm of constructors and `new' operators than
  63. Borland C++?  (for which code similar to this presents no problems).
  64. Is this paradigm written up anywhere?
  65.  
  66. Any pointers would be very much appreciated.  We can't figure out what
  67. the compiler is expecting of us.
  68.  
  69. (A general comment on the compiler implementation: ``too few
  70. parameters'' or ``too many parameters'' isn't really helpful as error
  71. messages; it would help to know how many parameters were being
  72. expected.)
  73.  
  74. Thanks!
  75. -- 
  76. ========================================================================
  77. A. Dain Samples, Dain.Samples@uc.edu, wk:(513)556-4783, hm:(513)771-5492
  78. ------------------------------------------------------------------------
  79. It is so difficult to find the beginning.  Or, better, it is difficult
  80. to begin at the beginning.  And not try to go further back.
  81.                       -Wittgentstein (from "On Certainty")
  82.  
  83. May the background be with you.
  84.                       -Heidegger (from "Being and Time", paraphrased)
  85.