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++ эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Demonstrate mutable.
#include <iostream>
using namespace std;
class MyClass {
mutable int i;
int j;
public:
int geti() const {
return i; // ok
}
void seti(int x) const {
i = x; // now, OK.
}
/* The following function won"t compile.
void setj(int x) const {
j = x; // Still Wrong!
}
*/
};
int main()
{
MyClass ob;
ob.seti(1900);
cout << ob.geti();
return 0;
}
1900"
Demonstrating storage-class specifier mutable
#include <iostream>
using std::cout;
using std::endl;
class MyClass {
public:
MyClass( int v )
{
value = v;
}
int getValue() const
{
return value++;
}
private:
mutable int value;
};
int main()
{
const MyClass test( 99 );
cout << "Initial value: " << test.getValue();
cout << "\nModified value: " << test.getValue() << endl;
return 0;
}
Initial value: 99
Modified value: 100