C++ Tutorial/Operator Overloading/overloading Dereference operator


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
Перейти к: навигация, поиск

Overloading De-reference operator

#include <iostream>
using std::cout;
using std::endl;
class MyType {
  public:
    MyType (int arg = 0) : x(arg) {}
    bool operator!=(const MyType& arg) const {
      return x != arg.x;
    }
    int operator*() const { return x; }     
    MyType& operator++() {                 
      ++x;
      return *this;
    }
    const MyType operator++(int) {         
      MyType temp(*this);                  
      ++x;
      return temp;                          
    }
  private: 
    int x;
};

template <typename Iter> 
double average(Iter a, Iter end) { 
  double sum = 0.0;
  for( ; a != end ;){
    cout << *a++;
    sum += *a++;
  }  
    
  return sum; 
} 
int main() {
  MyType first(1);
  MyType last(11);
  cout << average(first, last) << endl;
  return 0;
}
1357930