- 浏览: 388653 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15280首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 963RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 743It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 917As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1031typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 733There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 783we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 911As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 720There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 928Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 921As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 947As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1284in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 901class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 956template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1178There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 797the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 549The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1131In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 836A class member operator new() c ...
相关推荐
Laravel开发-eloquence-mutable 灵活的可搜索、可映射、元表、验证和更广泛的扩展,适用于大型雄辩ORM。
易变的环境,适用于Mutable Instruments模块黑客 该配置文件和此shellscript创建了一个Linux(ubuntu)虚拟机,该虚拟机配置有用于编译和安装Mutable Instruments模块的固件的所有正确工具。 荣誉和灵感 Adafruit的 ...
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++中修饰数据可变的关键字有三个:const、volatile和mutable。const比较好理解,表示其修饰的内容不可改变(至少编译期不可改变),而volatile和mutable恰好相反,指示数据总是可变的。mutable和volatile均可以和...
主要介绍了详解C++中mutable的用法,帮助大家更好的理解和学习C++,感兴趣的朋友可以了解下
: 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....
在C++中,mutable也是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中
在C++中,mutable也是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中。 我们知道,如果类的成员函数不会改变对象的状态,那么这个成员函数一般会声明成...
在C++中,mutable是为了突破const的限制而设置的。被mutable修饰的变量,将永远处于可变的状态,即使在一个const函数中,甚至结构体变量或者类对象为const,其mutable成员也可以被修改。mutable在类中只能够修饰非...
C++从零开始(十七)——C++中的一些生僻关键字(explicit、mutable、volatile等的说 明) C++从零开始(十八)——何谓SDK(说明为什么没有放音乐的指令却依然可以编出 放音乐的程序) C++从零开始(十九)——何谓...
先说用法,mutable关键字只能修饰非静态以及非常量成员变量,使用mutable修饰的成员变量在const函数中的值是可以修改的
C++中修饰数据可变的关键字有三个:const、volatile和mutable。const比较好理解,表示其修饰的内容不可改变(至少编译期不可改变),而volatile和mutable恰好相反,指示数据总是可变的。mutable和volatile均可以和...
这些新特征包括关键字 mutable 以及explicit。 有一些修改的潜在作用很大。现在 ANSI 规则鼓励某些编程方法而抛弃其他一些编程方 法。尽管在一段时间内不会放弃对老式编程方法的支持,但是有一些编程方法最终是要...
一些经过验证的用来改善程序效率的方法,包括检验C++... 介绍新的语言特性,包括bool、mutable、explicit、namespaces、成员模板、标准模板库等。如果你的编译器不支持这些特性,本书还介绍了如何不利用它们完成工作。
201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _...