home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / gnu / g / help / 1212 < prev    next >
Encoding:
Text File  |  1992-09-09  |  1.4 KB  |  50 lines

  1. Newsgroups: gnu.g++.help
  2. Path: sparky!uunet!sun-barr!ames!tulane!ukma!psuvax1!liblit
  3. From: liblit@cs.psu.edu (Benjamin R. Liblit)
  4. Subject: default copy constructor?
  5. Message-ID: <BuCs0n.5pF@cs.psu.edu>
  6. Sender: news@cs.psu.edu (Usenet)
  7. Nntp-Posting-Host: waldo.cs.psu.edu
  8. Distribution: gnu
  9. Date: Thu, 10 Sep 1992 09:09:54 GMT
  10. Lines: 38
  11.  
  12. I would like to construct an instance of a derived class using an
  13. instance of its base class.  The plan is to use the base class's copy
  14. constructor (a.k.a. "X(X&)") to initialize the inherited portions, and
  15. initialize the rest explicitly.  Thus:
  16.  
  17.     struct Base
  18.     {
  19.       int b;
  20.     };
  21.  
  22.     struct Derived : public Base
  23.     {
  24.       int d;
  25.   
  26.       Derived(const Base &shared)
  27.       : Base(shared),
  28.         d(1)
  29.         {
  30.         }
  31.     };
  32.  
  33. G++ 2.2.2 reports the following errors:
  34.  
  35.     try.cc: In method `Derived::Derived (const struct Base&)':
  36.     try.cc:13: no constructor found for initialization of `Base'
  37.  
  38. I had thought that *all* structures/classes/types had a default copy
  39. constructor defined for themselves.  This error seems to hint
  40. otherwise.  Am I confused or is G++?
  41.  
  42. If I do explicitly define a copy constructor for the base class, the
  43. errors disappear.  However, doing so in the real code I'm working on
  44. is not a feasible solution.
  45.  
  46. Any ideas?
  47. --
  48. Ben Liblit   -----   "Fais que tes reves soient plus longs que la nuit."
  49. liblit@cs.psu.edu   --   brl102@psuvm.psu.edu   --   brl102@psuvm.bitnet
  50.