xref: /llvm-project/lldb/test/API/functionalities/vtable/main.cpp (revision 7fbd427f5ebea4a4ebf25747758851875bb7e173)
1*7fbd427fSGreg Clayton class Shape {
2*7fbd427fSGreg Clayton public:
Area()3*7fbd427fSGreg Clayton   virtual double Area() { return 1.0; }
Perimeter()4*7fbd427fSGreg Clayton   virtual double Perimeter() { return 1.0; }
5*7fbd427fSGreg Clayton   // Note that destructors generate two entries in the vtable: base object
6*7fbd427fSGreg Clayton   // destructor and deleting destructor.
7*7fbd427fSGreg Clayton   virtual ~Shape() = default;
8*7fbd427fSGreg Clayton };
9*7fbd427fSGreg Clayton 
10*7fbd427fSGreg Clayton class Rectangle : public Shape {
11*7fbd427fSGreg Clayton public:
12*7fbd427fSGreg Clayton   ~Rectangle() override = default;
Area()13*7fbd427fSGreg Clayton   double Area() override { return 2.0; }
Perimeter()14*7fbd427fSGreg Clayton   double Perimeter() override { return 2.0; }
RectangleOnly()15*7fbd427fSGreg Clayton   virtual void RectangleOnly() {}
16*7fbd427fSGreg Clayton   // This *shouldn't* show up in the vtable.
RectangleSpecific()17*7fbd427fSGreg Clayton   void RectangleSpecific() { return; }
18*7fbd427fSGreg Clayton };
19*7fbd427fSGreg Clayton 
20*7fbd427fSGreg Clayton // Make a class that looks like it would be virtual because the first ivar is
21*7fbd427fSGreg Clayton // a virtual class and if we inspect memory at the address of this class it
22*7fbd427fSGreg Clayton // would appear to be a virtual class. We need to make sure we don't get a
23*7fbd427fSGreg Clayton // valid vtable from this object.
24*7fbd427fSGreg Clayton class NotVirtual {
25*7fbd427fSGreg Clayton   Rectangle m_rect;
26*7fbd427fSGreg Clayton public:
27*7fbd427fSGreg Clayton   NotVirtual() = default;
28*7fbd427fSGreg Clayton };
29*7fbd427fSGreg Clayton 
main(int argc,const char ** argv)30*7fbd427fSGreg Clayton int main(int argc, const char **argv) {
31*7fbd427fSGreg Clayton   Shape shape;
32*7fbd427fSGreg Clayton   Rectangle rect;
33*7fbd427fSGreg Clayton   Shape *shape_ptr = ▭
34*7fbd427fSGreg Clayton   Shape &shape_ref = shape;
35*7fbd427fSGreg Clayton   shape_ptr = &shape; // Shape is Rectangle
36*7fbd427fSGreg Clayton   NotVirtual not_virtual; // Shape is Shape
37*7fbd427fSGreg Clayton   return 0; // At the end
38*7fbd427fSGreg Clayton }
39