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

  1. Path: sparky!uunet!europa.asd.contel.com!darwin.sura.net!wupost!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!eff!ibmpcug!pipex!demon!cix.compulink.co.uk!vadim
  2. Newsgroups: comp.lang.c++
  3. From: vadim@cix.compulink.co.uk (Vadim Lebedev)
  4. Subject: Re: Renew?
  5. Cc: vadim@cix.compulink.co.uk
  6. Reply-To: vadim@cix.compulink.co.uk
  7. Date: Sun, 26 Jul 1992 19:04:09 +0000
  8. Message-ID: <memo.542965@cix.compulink.co.uk>
  9. Sender: usenet@gate.demon.co.uk
  10. Lines: 54
  11.  
  12. In-Reply-To: <23316@alice.att.com> ark@alice.att.com (Andrew Koenig)
  13.  
  14. Well, here are my 2 bits on operator renew.  How about following?:
  15.  
  16. class Renew { };
  17.  
  18. const Renew RENEW;
  19.  
  20. class ObjWithRenew
  21. {
  22. protected:
  23.  void virtual renewed(size_t) { }
  24. public:
  25.  void *operator new(size_t s) { return (void *) new char[s]; }
  26.  void *operator new(void *old, Renew , size_t newsize, size_t)
  27.  {
  28.   ObjWithRenew *p; 
  29.   p = (ObjWithRenew *)realloc(old, newsize);
  30.   p->renewed(s);  // We can do this because realloc copied
  31.            // the vptr too
  32.   return (void *)p;
  33.  }
  34. };  
  35.  
  36. // Now we can write
  37.  
  38.  
  39. class A: public ObjWithRenew
  40. {
  41.  void virtual renew(size_t) { /* whatever */ }
  42. public:
  43.  A(int a, b);
  44.  A(char *s);
  45.       
  46. };
  47.  
  48. void test()
  49. {
  50.  A *a1 = new A(2, 4);
  51.  
  52.         .....
  53.  size_t newSize = ....;
  54.  
  55.  A *a2 = new(a1, RENEW,  newSize) A("abc");
  56.  
  57. }
  58.  
  59.  
  60. -----------------------------------------------------------------------
  61. Vadim Lebedev                      |   Kortex International
  62. vadim@cix.compulink.co.uk          |   139-147 av. Paul-Vaillant Couturier
  63.                                    |   93126 La Courneuve - CEDEX, France
  64.  
  65.  
  66.