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++ эксперт
Operator as return type
#include <iostream>
using namespace std;
int p(int base, int exp);
class Power {
int base;
int exp;
public:
Power(int b, int e) {
base = b; exp = e;
}
operator int() {
return p(base, exp);
}
};
// Return base to the exp power.
int p(int base, int exp)
{
int temp;
for(temp=1; exp; exp--)
temp = temp * base;
return temp;
}
int main()
{
Power object1(2, 3), object2(3, 3);
int result;
result = object1;
cout << result << "\n";
result = object2;
cout << result << "\n";
cout << object1 + 100 << "\n";
return 0;
}