home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C22 / MultipleInheritance1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  858 b   |  41 lines

  1. //: C22:MultipleInheritance1.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // MI & ambiguity
  7. #include "../purge.h"
  8. #include <iostream>
  9. #include <vector>
  10. using namespace std;
  11.  
  12. class MBase {
  13. public:
  14.   virtual char* vf() const = 0;
  15.   virtual ~MBase() {}
  16. };
  17.  
  18. class D1 : public MBase {
  19. public:
  20.   char* vf() const { return "D1"; }
  21. };
  22.  
  23. class D2 : public MBase {
  24. public:
  25.   char* vf() const { return "D2"; }
  26. };
  27.  
  28. // Causes error: ambiguous override of vf():
  29. //! class MI : public D1, public D2 {};
  30.  
  31. int main() {
  32.   vector<MBase*> b;
  33.   b.push_back(new D1);
  34.   b.push_back(new D2);
  35.   // Cannot upcast: which subobject?:
  36. //!  b.push_back(new mi);
  37.   for(int i = 0; i < b.size(); i++)
  38.     cout << b[i]->vf() << endl;
  39.   purge(b);
  40. } ///:~
  41.