| 1 |
#include <assert.h> |
| 2 |
|
| 3 |
class A22351 |
| 4 |
{ |
| 5 |
public: |
| 6 |
virtual int f(); |
| 7 |
virtual int g(int *); |
| 8 |
virtual int h(); |
| 9 |
virtual int h() const; |
| 10 |
}; |
| 11 |
|
| 12 |
class B22351 : public A22351 |
| 13 |
{ |
| 14 |
public: |
| 15 |
virtual int f() const; |
| 16 |
virtual int g(const int *); |
| 17 |
int h() const override; |
| 18 |
}; |
| 19 |
|
| 20 |
B22351 *createB(); |
| 21 |
|
| 22 |
int main() |
| 23 |
{ |
| 24 |
// mutable A calls functions in A vtable |
| 25 |
A22351 *a = createB(); |
| 26 |
assert(a->f() == 1); |
| 27 |
assert(a->g(0) == 3); |
| 28 |
assert(a->h() == 5); |
| 29 |
|
| 30 |
// cast to B calls functions in B vtable |
| 31 |
B22351 *b = (B22351 *)a; |
| 32 |
assert(b->f() == 2); |
| 33 |
assert(b->g(0) == 4); |
| 34 |
assert(b->h() == 6); |
| 35 |
|
| 36 |
// cast to const calls B override function |
| 37 |
const A22351 *ca = a; |
| 38 |
assert(ca->h() == 6); |
| 39 |
|
| 40 |
// const B calls functions in B vtable |
| 41 |
const B22351 *cb = createB(); |
| 42 |
assert(cb->f() == 2); |
| 43 |
assert(cb->h() == 6); |
| 44 |
|
| 45 |
return 0; |
| 46 |
} |