Commit bc59b51ad6b3ad796f4c3f789dd084fda1a8ec95
1 parent
982549f4
Casting examples
Showing
13 changed files
with
106 additions
and
0 deletions
examples10/05-ptrconv/makefile
0 → 100644
examples10/05-ptrconv/ptrconv1.cpp
0 → 100644
examples10/05-ptrconv/ptrconv2.cpp
0 → 100644
| 1 | +class A { | |
| 2 | +public: | |
| 3 | + int a; | |
| 4 | +}; | |
| 5 | + | |
| 6 | +class B : virtual public A { | |
| 7 | + int b; | |
| 8 | +}; | |
| 9 | + | |
| 10 | +class C : virtual public A { | |
| 11 | + int c; | |
| 12 | +}; | |
| 13 | + | |
| 14 | +class D: public B, public C | |
| 15 | +{ | |
| 16 | + int d; | |
| 17 | +}; | |
| 18 | + | |
| 19 | +class E: public C, public B | |
| 20 | +{ | |
| 21 | + int e; | |
| 22 | +}; | |
| 23 | + | |
| 24 | +int main() | |
| 25 | +{ | |
| 26 | + D d; | |
| 27 | + E e; | |
| 28 | + | |
| 29 | + C* cptr1 = &d; | |
| 30 | + C* cptr2 = &e; | |
| 31 | + | |
| 32 | + cptr1->a = 1; | |
| 33 | + cptr2->a = 2; | |
| 34 | + | |
| 35 | +} | |
| 36 | + | ... | ... |
examples11/01-cast/cast.cpp
0 → 100644
| 1 | +class Storable { | |
| 2 | +public: | |
| 3 | +virtual ~Storable(){}; | |
| 4 | +}; | |
| 5 | + | |
| 6 | +class Component : public virtual Storable | |
| 7 | +{ /* ... */ }; | |
| 8 | +class Receiver : public Component | |
| 9 | +{ /* ... */ }; | |
| 10 | +class Transmitter : public Component | |
| 11 | +{ /* ... */ }; | |
| 12 | +class Radio : public Receiver, public Transmitter | |
| 13 | +{ /* ... */ }; | |
| 14 | + | |
| 15 | +int main() | |
| 16 | +{ | |
| 17 | + Radio r; | |
| 18 | + | |
| 19 | + Storable* s = &r; | |
| 20 | + | |
| 21 | + Transmitter* t = dynamic_cast<Transmitter*> (&r); | |
| 22 | + | |
| 23 | + Component* c = t; | |
| 24 | + | |
| 25 | + Radio* rptr = dynamic_cast<Radio*>(c); | |
| 26 | + | |
| 27 | +} | ... | ... |
examples11/01-cast/makefile
0 → 100644
examples11/01-funptr/funptr1.cpp renamed to examples11/02-funptr/funptr1.cpp
examples11/01-funptr/funptr2.cpp renamed to examples11/02-funptr/funptr2.cpp
examples11/01-funptr/makefile renamed to examples11/02-funptr/makefile
examples11/02-new/makefile renamed to examples11/03-new/makefile
examples11/02-new/newhandler.cpp renamed to examples11/03-new/newhandler.cpp
examples11/02-new/placement2.cpp renamed to examples11/03-new/placement2.cpp
examples11/02-new/placementnew.cpp renamed to examples11/03-new/placementnew.cpp
examples11/02-new/placementnew2.cpp renamed to examples11/03-new/placementnew2.cpp