SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <iostream> | |
| 2 | #include <vector> | |
| 3 | ||
| 4 | using namespace std; | |
| 5 | ||
| 6 | - | class Base; |
| 6 | + | |
| 7 | - | class A; |
| 7 | + | |
| 8 | - | struct BaseVisitor |
| 8 | + | virtual bool is (int value) const |
| 9 | - | {
|
| 9 | + | {
|
| 10 | - | virtual void visit (Base&) = 0; |
| 10 | + | return av == value; |
| 11 | - | virtual void visit (A&) = 0; |
| 11 | + | |
| 12 | virtual void print() const | |
| 13 | {
| |
| 14 | cout << av << endl; | |
| 15 | - | virtual void accept (BaseVisitor& v) {
|
| 15 | + | |
| 16 | - | v.visit (*this); |
| 16 | + | |
| 17 | ||
| 18 | struct A : public Base {
| |
| 19 | int bv; | |
| 20 | virtual bool is (int value) const | |
| 21 | {
| |
| 22 | - | virtual void accept (BaseVisitor& v) {
|
| 22 | + | return bv == value; |
| 23 | - | v.visit (*this); |
| 23 | + | |
| 24 | virtual void print () const | |
| 25 | {
| |
| 26 | cout << bv << endl; | |
| 27 | - | struct ValueFinder: public BaseVisitor {
|
| 27 | + | |
| 28 | - | int value; |
| 28 | + | |
| 29 | - | bool found; |
| 29 | + | |
| 30 | - | ValueFinder (int value) : value(value) {}
|
| 30 | + | |
| 31 | - | virtual void visit (Base& b) {
|
| 31 | + | |
| 32 | - | found = value == b.av; |
| 32 | + | |
| 33 | database () { NBase.av = 0; }
| |
| 34 | - | virtual void visit (A& b) {
|
| 34 | + | |
| 35 | - | found = value == b.bv; |
| 35 | + | |
| 36 | for (int i = 0; i < basev.size (); ++i) {
| |
| 37 | if(basev[i]->is (value)); | |
| 38 | return basev[i]; | |
| 39 | } | |
| 40 | return &NBase; | |
| 41 | } | |
| 42 | } db; | |
| 43 | ||
| 44 | int main( ) {
| |
| 45 | ||
| 46 | - | ValueFinder finder(value); |
| 46 | + | |
| 47 | der.av = 5; | |
| 48 | - | basev[i]->accept (finder); |
| 48 | + | |
| 49 | - | if(finder.found) |
| 49 | + | |
| 50 | Base* found = db.findb (5); | |
| 51 | found->print (); | |
| 52 | } |