language: C++ 4.7.2 (gcc-4.7.2)
date: 616 days 10 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
struct Shape
{
    struct subtype { enum { Shape, Circle, Rectangle, ColoredCircle }; };
 
    virtual bool is_a( int type ) const { return type == subtype::Shape; }
    virtual bool is_equal(const Shape& s) const { return false; }
};
 
struct Rectangle : Shape
{
    virtual bool is_a( int type ) const { return type == subtype::Rectangle || Shape::is_a(type); }
    virtual bool is_equal(const Shape& s) const
    {
        if (!s.is_a(subtype::Rectangle)) return false;
        const Rectangle& r = static_cast<const Rectangle&>(s);
        return true; // or check width and height
    }
};
 
struct Circle : Shape
{
    virtual bool is_a( int type ) const { return type == subtype::Circle || Shape::is_a(type); }
    virtual bool is_equal(const Shape& s) const
    {
        if (!s.is_a(subtype::Circle)) return false;
        const Circle& c = static_cast<const Circle&>(s);
        return true; // or check radius
    }
};
 
struct ColoredCircle : Circle
{
    virtual bool is_a( int type ) const { return type == subtype::ColoredCircle || Circle::is_a(type); }
};
 
int main(void)
{
    Rectangle x;
    Shape y;
    return x.is_equal(y);
}
 
prog.cpp: In member function ‘virtual bool Rectangle::is_equal(const Shape&) const’:
prog.cpp:15: warning: unused variable ‘r’
prog.cpp: In member function ‘virtual bool Circle::is_equal(const Shape&) const’:
prog.cpp:26: warning: unused variable ‘c’