- 浏览: 399758 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
In the previeous post, c++ special member functions- const and volatile members we have discussed some speical members, such as the constant members and the volatile members, now let's see a special members - which is called the mutable members.
when we are talking about the mutable members, we have to talk aobut the const members, when const object calls the const members, one constraint imposed by the const member classes is that they does not allow to modify the class members. such as
const Screen cs(5, 5) cs.move(3,4) char ch = cs.get();
However, the move method does change the internal state of the object
inline void Screen::move(int r, int c) { if (checkRange(r, c) ) { int row = (r - 1) * _width; _cursor = row + c - 1; } }
you will end up in the compiler time error.
to allow a class member to be modified even though it is the dat member of a const object. we can declare the data member as mutable.
such as
// from the above declaration below, to allow the move method to be called from a const object //string::size_type _cursor; mutable string::size_type _cursor;
below is the full declaration of the Screen class which you can try on your c++ code on the const and mutable classes.
#include <string> #include <iostream> #include <fstream> #include <functional> #include <iterator> #include <algorithm> #include <cstring> using std::string; using std::fstream; using std::copy; using std::cout; using std::endl; using std::cerr; using std::strcpy; using std::strcat; using std::istream; using std::ostream; class Screen { public: // constructor inline Screen(int hi = 8, int wid = 40, char bk = '*'); void home() { _cursor = 0; } void move(int, int); void move(int , int ) const; char get() { return _screen[_cursor]; } inline char get(int, int ); bool checkRange(int, int) const; int height() { return _height; } int width() { return _width;} // not defined, we might go to that later. friend istream& operator >>(istream &, Screen &) ; friend ostream& operator <<(ostream&, const Screen &) ; void copy(const Screen & obj); inline void set(const string &s); inline void set(char s); private: inline int remainingSpace(); string _screen; // from the above declaration below, to allow the move method to be called from a const object //string::size_type _cursor; mutable string::size_type _cursor; int _height; int _width; /*static const int _height = 24; static const int _width =80;*/ }; void Screen::copy(const Screen &obj) { /// fif this Screen object and objs are the same objcet // no copy necessary /// we look at hte 'this ' pointer if (this != &obj) { _height = obj._height; _width =obj._width; _cursor = 0; // create a new string // its content is the same as obj._screen _screen = obj._screen; } } bool Screen::checkRange(int row, int col) const { if (row < 1 || row > _height || col < 1 || col > _width ) { cerr << "Screen coordinates (" << row << ", " << col << " ) out of bounds.\n"; return false; } return true; // a better way is to write as such //return !((row < 1 || row > _height || col < 1 || col > _width)); } inline void Screen::move(int r, int c) { // move _cursor to absolute position if (checkRange(r, c) ) { int row = ( r - 1) * _width; // row location _cursor = row + c - 1; } } inline void Screen::move(int r, int c) const { // can we directly call the Screen::move method or can we ask the Screen::move(int r, int c) to call the (Screen::move(int r, int c) const" method if (checkRange(r, c)) { int row = (r - 1) * _width; _cursor = row + c - 1; // because now the _cursor is mutable members, it does not matter if you call it from a const contetxt } } // a side note, inline declaration should be placed // in the header file. normally you will carry the inline keyword with you . inline char Screen::get(int r, int c) { move(r, c); return get(); } inline void Screen::set(const string &s) { // write string beginning at current _cursor position int space = remainingSpace(); int len = s.size(); if (space < len) { cerr << "Screen: warning: truncate: " << "space: " << space << "string length: " << len << endl; } } inline void Screen::set(char ch) { if (ch == '\0') { cerr << "Screen: Warning: " << "Null character (ignored).\n"; } else { _screen[_cursor] = ch; } } inline int Screen::remainingSpace() { // currrent position is no longer remaining int sz = _width * _height; return (sz - _cursor); } inline Screen::Screen(int hi, int wid, char bk) : _height (hi), _width(wid), _cursor(0), _screen(hi * wid, bk) { // all the work is done with the number initialize list } void test_screen1() { Screen sobj(3, 3); string init("abcdefghi"); cout << "Screen object )" << sobj.height() << " , " << sobj.width() << " ) \n\n"; // Set the content of hte screen string::size_type initops = 0; for (int ix = 1; ix <= sobj.width(); ++ix) { for (int iy = 1; iy <= sobj.height(); ++iy) { sobj.move(ix, iy); sobj.set(init[initops++]); } } // print the conent of the screen for (int ix = 1; ix <= sobj.width(); ++ix) { for (int iy = 1; iy <= sobj.height(); ++iy) { cout << sobj.get(ix, iy); } cout << "\n"; } } void test_const_mutable() { const Screen cs(5, 5); // however, now you cannot call the move method, because the move method change the _cursor // to make that happen, you have to change the type of the _cursor // from // string::size_type _cursor; // // to // mutable string::size_type _cursor; cs.move(3, 4); }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15342首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1012RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 772It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 963As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1060typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 771There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 823we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 977As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 754There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 960Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 975As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 994As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1328in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 939class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 986template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1212There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 854the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 588The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1170In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 873A class member operator new() c ...
相关推荐
"Laravel开发-eloquence-mutable"项目就是为了这样的目的而设计的,它为Eloquent添加了更多实用特性,提高了ORM的灵活性和实用性。 1. **可搜索性**: eloquence-mutable扩展增强了Eloquent模型的搜索功能。它允许...
C++中修饰数据可变的关键字有三个:const、volatile和mutable。const比较好理解,表示其修饰的内容不可改变(至少编译期不可改变),而volatile和mutable恰好相反,指示数据总是可变的。mutable和volatile均可以和...
易变的环境,适用于Mutable Instruments模块黑客 该配置文件和此shellscript创建了一个Linux(ubuntu)虚拟机,该虚拟机配置有用于编译和安装Mutable Instruments模块的固件的所有正确工具。 荣誉和灵感 Adafruit的 ...
- **通用lambda**:lambda表达式可以有默认参数和捕获列表中的mutable关键字。 - **变长模板参数(Variadic Templates)**:允许模板接受任意数量的参数,常用于实现可变参数模板函数。 - **用户定义字面量(User-...
Mutable Instruments Ambika的案例和面板文件 笔记: 已将Panel SVG文件设置为在svg2shenzhen扩展名的Inkscape中使用,以导出Gerber。 机箱+脸颊文件有两种类型,具体取决于在顶面板和主板之间使用的长度支架。 ...
Shruthi XT机箱 我的木制Shruthi XT外壳的设计和零件,可以选择在激光上雕刻有灵感源自原始设计的图形。 零件设计为在CNC铣床上粗切,然后手工精加工和修整。档案文件该计划最初是使用Mac版Eazydraw绘制的。...
青少年辫子Mutable Instruments Braids的端口连接到Teensy板该项目提取Mutable Instruments Braids宏振荡器,并使用teensy的模拟输出(引脚A14)作为音频输出。 闪烁后,Teensy会接收MIDI-USB消息。 振荡器连续发出...
C++中volatile和mutable关键字用法详解 C++中volatile关键字的用法详解: volatile关键字是C++中的一种类型限定符,它用于告诉编译器该变量值是不稳定的,可能被更改。使用volatile注意事项有六点: 1. 编译器会...
- **语法**: `[capture](parameters) mutable|exception return-type -> { body }`。 - **特点**: 可以捕获外围作用域中的变量,支持闭包。 ### 10. 其他特性 - **auto**: 推断类型,提高代码的可读性和可维护性。...
: Construct The Comma Operator Overloading the ( ) Operator Pointers to Members The asm Statement The mutable Qualifier Run Time Type Identification Trigraphs Answers to Chapter Questions 30....
mutable是C++中用于标示某个成员变量可以被const成员函数修改的修饰符。即使该对象为常量,我们仍然可以修改这个成员变量的值。以下是关于C++中mutable用法的详细知识点。 1. **mutable的含义与作用** - mutable是...
在C++编程语言中,`mutable`是一个特殊的关键字,它的主要作用是允许在const成员函数内部修改对象的成员变量。通常,const成员函数承诺不修改对象的状态,但`mutable`关键字提供了一种机制,使得程序员可以在保持...
在C++中,mutable也是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中。 我们知道,如果类的成员函数不会改变对象的状态,那么这个成员函数一般会声明成...
在C++中,mutable是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中,甚至结构体变量或者类对象为const,其mutable成员也可以被修改。mutable在类中只能够修饰非...
在C++编程语言中,`mutable`关键字是一个特殊的关键字,它的主要作用是允许在const成员函数中修改对象的某些特定成员变量。这在某些情况下非常有用,因为const成员函数通常承诺不改变对象的状态,但有时我们需要在...
- **mutable**:这是一个非常有用的关键字,它允许在一个`const`对象内部改变某个成员变量的值。这在多线程编程和缓存实现等方面有着重要的应用。 - **explicit**:这个关键字用于构造函数或转换操作符,以防止隐式...
java java_leetcode题解之Range Sum Query - Mutable.java
C++ 是一种强大的编程语言,尤其适合开发系统软件、应用程序和游戏等。本文将深入讲解C++的基础知识,包括环境设置、基本语法、数据类型、变量、运算符、流程控制、函数、数组、字符串、指针、引用以及日期和时间等...
java java_leetcode题解之Range Sum Query 2D - Mutable.java