Advertisement
Carcigenicate

Untitled

Jan 21st, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct Base {
  8.     string type;
  9.     int av;
  10.    
  11.     Base () { av = 2; type = "Base"; }
  12. };
  13.  
  14. struct A : public Base {
  15.     int bv;
  16.    
  17.     A () { av = 1; bv = 3; type = "A"; }
  18. };
  19.  
  20. struct database {
  21.    
  22.     vector <Base*> basev;
  23.    
  24.     Base* findb (int value) {
  25.         static Base sbase; Base* pbase = &sbase;
  26.         pbase->type = "nothing";
  27.        
  28.         for (int i = 0; i < basev.size (); ++i) {
  29.             if (basev [i]->av == value) return basev [i];
  30.         }
  31.         return pbase;
  32.     }
  33.    
  34. } db;
  35.    
  36.  
  37. int main() {
  38.    
  39.     Base* pBase = new Base;
  40.     A* pA = new A;
  41.    
  42.     pBase->av = 5;
  43.     pA->av = 2;
  44.    
  45.     db.basev.push_back (pBase);
  46.     db.basev.push_back (pA);
  47.    
  48.     Base* found = db.findb (2);
  49.  
  50.     if (found->av > -1) {
  51.         cout << "Found " << found->type << endl;
  52.     }
  53.    
  54.     delete pBase, pA;
  55.    
  56.     cout << "\nDone";
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement