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

  1. Path: sparky!uunet!sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uwm.edu!lll-winken!taurus!taygeta.oc.nps.navy.mil!skip
  2. From: skip@taygeta.oc.nps.navy.mil (Skip Carter)
  3. Newsgroups: comp.lang.c++
  4. Subject: copy ctor and inheritance
  5. Message-ID: <6143@taurus.cs.nps.navy.mil>
  6. Date: 3 Sep 92 17:41:37 GMT
  7. Sender: news@taurus.cs.nps.navy.mil
  8. Reply-To: skip@taygeta.oc.nps.navy.mil (Skip Carter)
  9. Lines: 72
  10.  
  11.  
  12.     How do I get the copy constructor of a base class to get invoked
  13.     when I use a copy constructor for a base class ?
  14.  
  15.     In the following example I get the behaviour I want (AT&T 2.1)
  16.     if I leave it up to the compiler to create the copy constructor for B,
  17.     but if I write my own, the copy constructor for the base class A never
  18.     gets invoked.  (It does not matter whether or not I define the copy constructor
  19.     for the base class, A).
  20.  
  21.  
  22. #include <iostream.h>
  23.  
  24. class A
  25. {
  26.     private:
  27.       int val;
  28.     public:
  29.       A() : val(0) { cout << "A() invoked\n"; }
  30.       A(const A& a) { val = a.val; cout << "A(&A) invoked,  val = " << val << '\n'; }
  31.      ~A() { cout << "A.val = " << val << " going away\n"; }
  32.       void set_val(const int v) { val = v; }
  33.       int get_val() const { return val; }
  34. };
  35.  
  36. class B : public A
  37. {
  38.     private:
  39.        int vv;
  40.     public:
  41.       B() : vv(0) { cout << "B() invoked\n"; }
  42.  
  43.                       // If the constructor below is commented out, A::val is copied
  44.     //    B(const B& b) { vv = b.vv; cout << "B(&B) invoked\n"; }
  45.  
  46.      ~B() { cout << "B.val = " << vv << '\t' << get_val() << " going away\n"; }
  47.       void set_v(const int v) { vv = v; }
  48.       int get_v() const { return vv; }
  49. };
  50.  
  51. void main()
  52. {
  53.     A a;
  54.  
  55.     a.set_val( 3 );
  56.  
  57.     B b;
  58.  
  59.     b.set_v( 2 );
  60.  
  61.     b.set_val( 1 );
  62.  
  63.     // no surprises here
  64.     cout << "A = " << a.get_val() << endl;
  65.     cout << "B = " << b.get_v() << '\t' << b.get_val() << endl;
  66.  
  67.  
  68.     // now for the real test
  69.  
  70.     B c( b );
  71.  
  72.     cout << "C = " << c.get_v() << '\t' << c.get_val() << endl;
  73.  
  74. }
  75.  
  76.  
  77. -- 
  78.  
  79.  Everett (Skip) Carter             Phone:  408-646-3318 FAX: 408-646-2712
  80.  Naval Postgraduate School         INTERNET: skip@taygeta.oc.nps.navy.mil
  81.  Dept. of Oceanography, Code OC/CR  UUCP:     ...!uunet!taygeta!skip
  82.  Monterey, CA. 93943               TELEMAIL: s.carter/omnet
  83.