home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 13293 < prev    next >
Encoding:
Text File  |  1992-09-04  |  1.3 KB  |  38 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.mentorg.com!sdl!adk
  3. From: adk@Warren.MENTORG.COM (Ajay Kamdar)
  4. Subject: Re: copy ctor and inheritance
  5. Message-ID: <1992Sep4.143644.5075@Warren.MENTORG.COM>
  6. Organization: Mentor Graphics Corp. - IC Group
  7. References: <6143@taurus.cs.nps.navy.mil>
  8. Date: Fri, 4 Sep 1992 14:36:44 GMT
  9. Lines: 27
  10.  
  11. In article <6143@taurus.cs.nps.navy.mil> skip@taygeta.oc.nps.navy.mil (Skip Carter) writes:
  12. >
  13. >    How do I get the copy constructor of a base class to get invoked
  14. >    when I use a copy constructor for a base class ?
  15. >
  16. >    In the following example I get the behaviour I want (AT&T 2.1)
  17. >    if I leave it up to the compiler to create the copy constructor for B,
  18. >    but if I write my own, the copy constructor for the base class A never
  19. >    gets invoked.  (It does not matter whether or not I define the copy constructor
  20. >    for the base class, A).
  21.  
  22. >
  23. >    // If the constructor below is commented out, A::val is copied
  24. >    //    B(const B& b) { vv = b.vv; cout << "B(&B) invoked\n"; }
  25.  
  26. B's copy constructor should be written as:
  27.  
  28.     B(const B& b) : A(b) { vv = b.vv; cout << "B(&B) invoked\n"; }
  29.  
  30.  
  31. - Ajay
  32.  
  33. -- 
  34. I speak for none but myself.
  35.  
  36. Ajay Kamdar                               Email : ajay_kamdar@mentorg.com
  37. Mentor Graphics, IC Group (Warren, NJ)    Phone : (908) 580-0102
  38.