fork(1) download
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. //////////////////////////////////////////////////////////////////////////////////
  5.  
  6. struct Base1
  7. {
  8. Base1( unsigned& val ) : m_val( val ){}
  9. bool operator==( const Base1& rhs )const { return m_val == rhs.m_val; }
  10. unsigned& m_val;
  11. };
  12.  
  13. //////////////////////////////////////////////////////////////////////////////////
  14.  
  15. struct Base2
  16. {
  17. Base2( unsigned& val ) : m_val( val ){}
  18. bool operator==( const Base2& rhs ) const { return m_val == rhs.m_val; }
  19. unsigned& m_val;
  20. };
  21.  
  22. //////////////////////////////////////////////////////////////////////////////////
  23.  
  24. class Derived : public Base1 , public Base2 // Real problem has many more more Base classes
  25. {
  26. public:
  27. Derived( unsigned& val1 , unsigned& val2 ) : Base1( val1 ) , Base2( val2 )
  28. {
  29. }
  30.  
  31. bool operator==( const Derived& rhs ) const // How to generalize this
  32. {
  33. const Base1& rhsBase1 = rhs;
  34. const Base2& rhsBase2 = rhs;
  35.  
  36. const Base1& thisBase1 = *this;
  37. const Base2& thisBase2 = *this;
  38.  
  39. return ( thisBase1 == rhsBase1 ) && ( thisBase2 == rhsBase2 );
  40. }
  41. };
  42.  
  43. //////////////////////////////////////////////////////////////////////////////////
  44.  
  45. int main()
  46. {
  47. unsigned val1 = 42 , val2 = 24 , val3 = 0;
  48. Derived d1( val1 , val2 );
  49. Derived d2( val1 , val3 );
  50.  
  51. std::cout << ( d1 == d2 ) << std::endl;
  52. return EXIT_SUCCESS;
  53. }
  54.  
  55. //////////////////////////////////////////////////////////////////////////////////
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
0