Home | History | Annotate | Line # | Download | only in gdb.cp
      1  1.1  christos 
      2  1.1  christos #include <stdio.h>
      3  1.1  christos 
      4  1.1  christos class Base
      5  1.1  christos {
      6  1.1  christos public:
      7  1.1  christos   Base(int k);
      8  1.1  christos   ~Base();
      9  1.1  christos   virtual void foo() {}
     10  1.1  christos private:
     11  1.1  christos   int k;
     12  1.1  christos };
     13  1.1  christos 
     14  1.1  christos Base::Base(int k)
     15  1.1  christos {
     16  1.1  christos   this->k = k;
     17  1.1  christos }
     18  1.1  christos 
     19  1.1  christos Base::~Base()
     20  1.1  christos {
     21  1.1  christos     printf("~Base\n");
     22  1.1  christos }
     23  1.1  christos 
     24  1.1  christos class Derived : public virtual Base
     25  1.1  christos {
     26  1.1  christos public:
     27  1.1  christos   Derived(int i);
     28  1.1  christos   ~Derived();
     29  1.1  christos private:
     30  1.1  christos   int i;
     31  1.1  christos   int i2;
     32  1.1  christos };
     33  1.1  christos 
     34  1.1  christos Derived::Derived(int i) : Base(i)
     35  1.1  christos {
     36  1.1  christos   this->i = i;
     37  1.1  christos   /* The next statement is spread over two lines on purpose to exercise
     38  1.1  christos      a bug where breakpoints set on all but the last line of a statement
     39  1.1  christos      would not get multiple breakpoints.
     40  1.1  christos      The second line's text for gdb_get_line_number is a subset of the
     41  1.1  christos      first line so that we don't care which line gdb prints when it stops.  */
     42  1.1  christos   this->i2 = // set breakpoint here
     43  1.1  christos     i; // breakpoint here
     44  1.1  christos }
     45  1.1  christos 
     46  1.1  christos Derived::~Derived()
     47  1.1  christos {
     48  1.1  christos     printf("~Derived\n");
     49  1.1  christos }
     50  1.1  christos 
     51  1.1  christos class DeeplyDerived : public Derived
     52  1.1  christos {
     53  1.1  christos public:
     54  1.1  christos   DeeplyDerived(int i) : Base(i), Derived(i) {}
     55  1.1  christos };
     56  1.1  christos 
     57  1.1  christos int main()
     58  1.1  christos {
     59  1.1  christos   /* Invokes the Derived ctor that constructs both
     60  1.1  christos      Derived and Base.  */
     61  1.1  christos   Derived d(7);
     62  1.1  christos   /* Invokes the Derived ctor that constructs only
     63  1.1  christos      Derived. Base is constructed separately by
     64  1.1  christos      DeeplyDerived's ctor.  */
     65  1.1  christos   DeeplyDerived dd(15);
     66  1.6  christos 
     67  1.7  christos   Derived *dyn_d = new Derived (24);
     68  1.7  christos   DeeplyDerived *dyn_dd = new DeeplyDerived (42);
     69  1.7  christos 
     70  1.7  christos   delete dyn_d;
     71  1.7  christos   delete dyn_dd;
     72  1.7  christos 
     73  1.6  christos   return 0;
     74  1.1  christos }
     75