Examining C++ Program Analyzers
by Scott Meyers and Martin Klaus


Example 1: 

BaseClassDtor (
  Class B;
  Class D | D.is_descendant(B);
   Assert(MemberFunction B::m; | m.name() == "~" +  B.name() && m.is_virtual());
);



Listing One
//  20  M24  S   Avoid gratuitious use of virtual inheritance, i.e., make
//  sure there are at least two inheritance paths to each virtual base class.
class Base { int x; };
class Derived: virtual public Base {};
Derived d;


Listing Two
class B { ... };             // base class;  assume no virtual dtor
class D: public B { ... };   // derived class
void f(B *p);                // f is some function taking a B*
D *pd = new D;               // pd points to a D
f(pd);                       // pass pd to f, binding pd to p in f
void f(B *p)
  {
    delete p;                // this calls only B's dtor, not D's!
  }


Listing Three
// test program for rule 10

class Base {};
class Derived: public Base {};


int main()
{
  Base *pb = new Derived;
  delete pb;
  return 0;
}

1



