mutable是为了突破const函数的限制而设计的,mutable修饰的成员变量将永远处于可改变的状态。mutable成员变量破坏了只读对象的内部状态,而const成员函数保证只读对象的状态不变性,所以mutable成员变量没法保证只读对象状态的不变性。ios
#include <iostream> using namespace std; class Test { public: Test():m_count(0) { } void setValue(const int value) { m_count++; m_data = value; } int getValue() { m_count++; return m_data; } int getValue()const { m_count++; return m_data; } int getCount()const { return m_count; } private: int m_data; mutable int m_count; }; int main(int argc, char *argv[]) { Test test1; test1.setValue(100); cout << test1.getCount() << endl; const Test test2; test2.getValue(); cout << test2.getCount() << endl; return 0; }
上述代码使用mutable修饰成员变量m_count,确保在const函数内部也能够改变其值,但mutable破坏了只读对象状态的不变性,因此不推荐。数组
#include <iostream> using namespace std; class Test { public: Test():m_count(new int(0)) { } void setValue(const int value) { *m_count = *m_count + 1; m_data = value; } int getValue() { *m_count = *m_count + 1; return m_data; } int getValue()const { *m_count = *m_count + 1; return m_data; } int getCount()const { return *m_count; } private: int m_data; int* const m_count; }; int main(int argc, char *argv[]) { Test test1; test1.setValue(100); cout << test1.getCount() << endl; const Test test2; test2.getValue(); cout << test2.getCount() << endl; return 0; }
上述代码使用指针常量统计访问成员变量的次数,不会破坏只读对象的状态不变性。ide
new/delete的本质是C++预约义的操做符,C++语言规范对new/delete操做符作出了严格的规范:
A、new关键字用于获取足够的内存空间(默认为堆空间),在获取的空间中调用构造函数建立对象。
B、delete调用析构函数销毁对象,归还对象所占用的空间(默认为堆空间)。
C++语言中能够重载new/delete操做符,重载new/delete操做符的意义在于改变更态对象建立时的内存分配方式,能够将new建立的对象分配在栈空间、静态存储空间、指定地址空间。
new/delete操做符支持全局重载、局部重载,但不推荐对new/delete操做符进行全局重载,一般对new/delete操做符进行局部重载,如针对具体的类进行new/delete操做符重载。
new/delete操做符重载函数默认为静态函数,不管是否显示声明static关键字。函数
//static member function void* operator new(unsigned int size) { void* ret = NULL; /* ret point to allocated memory */ return ret; } //static member function void operator delete(void* p) { /* free the memory which is pointed by p */ }
#include <iostream> using namespace std; class Test { private: static const unsigned int COUNT = 4; static char c_buffer[]; static char c_map[]; int m_value; public: Test(int value = 0) { m_value = value; cout << "value : " << value << endl; } //static member function void* operator new (unsigned int size) { void* ret = NULL; for(int i = 0; i < COUNT; i++) { if( !c_map[i] ) { c_map[i] = 1; ret = c_buffer + i * sizeof(Test); cout << "succeed to allocate memory: " << ret << endl; break; } } return ret; } //static member function void operator delete (void* p) { if( p != NULL ) { char* mem = reinterpret_cast<char*>(p); int index = (mem - c_buffer) / sizeof(Test); int flag = (mem - c_buffer) % sizeof(Test); if( (flag == 0) && (0 <= index) && (index < COUNT) ) { c_map[index] = 0; cout << "succeed to free memory: " << p << endl; } } } int getValue()const { return m_value; } }; char Test::c_buffer[sizeof(Test) * Test::COUNT] = {0}; char Test::c_map[Test::COUNT] = {0}; int main(int argc, char *argv[]) { cout << "===== Test Single Object =====" << endl; Test* pt = new Test(1); delete pt; cout << "===== Test Object Array =====" << endl; Test* pa[5] = {0}; for(int i=0; i<5; i++) { pa[i] = new Test(100 + i); cout << "pa[" << i << "] = " << pa[i] << endl; } for(int i=0; i<5; i++) { cout << "delete " << pa[i] << endl; if(pa[i] != NULL) { delete pa[i]; } } return 0; }
上述代码,new会建立Test对象到静态存储空间中,从打印结果能够知道new建立Test对象时先调用new操做符重载函数,在返回的空间中再调用Test构造函数。学习
在类中对new/delete操做符进行重载,在new操做符重载函数中返回指定的地址,在delete操做符重载函数中标记对应的地址可用。优化
#include <iostream> #include <string> #include <cstdlib> using namespace std; class Test { static unsigned int c_count; static char* c_buffer; static char* c_map; int m_value; public: static bool SetMemorySource(char* memory, unsigned int size) { bool ret = false; c_count = size / sizeof(Test); ret = (c_count && (c_map = reinterpret_cast<char*>(calloc(c_count, sizeof(char))))); if( ret ) { c_buffer = memory; } else { free(c_map); c_map = NULL; c_buffer = NULL; c_count = 0; } return ret; } void* operator new (unsigned int size) { void* ret = NULL; if( c_count > 0 ) { for(int i=0; i<c_count; i++) { if( !c_map[i] ) { c_map[i] = 1; ret = c_buffer + i * sizeof(Test); cout << "succeed to allocate memory: " << ret << endl; break; } } } else { ret = malloc(size); } return ret; } void operator delete (void* p) { if( p != NULL ) { if( c_count > 0 ) { char* mem = reinterpret_cast<char*>(p); int index = (mem - c_buffer) / sizeof(Test); int flag = (mem - c_buffer) % sizeof(Test); if( (flag == 0) && (0 <= index) && (index < c_count) ) { c_map[index] = 0; cout << "succeed to free memory: " << p << endl; } } else { free(p); } } } }; unsigned int Test::c_count = 0; char* Test::c_buffer = NULL; char* Test::c_map = NULL; int main(int argc, char *argv[]) { char buffer[12] = {0}; Test::SetMemorySource(buffer, sizeof(buffer)); cout << "===== Test Single Object =====" << endl; Test* pt = new Test; delete pt; cout << "===== Test Object Array =====" << endl; Test* pa[5] = {0}; for(int i=0; i<5; i++) { pa[i] = new Test; cout << "pa[" << i << "] = " << pa[i] << endl; } for(int i=0; i<5; i++) { cout << "delete " << pa[i] << endl; delete pa[i]; } return 0; }
上述代码中,能够在指定地址空间建立对象,也能够不指定地址空间,此时在堆空间建立对象。this
#include <iostream> using namespace std; class Test { int m_value; public: Test(int value = 0) { m_value = value; cout << "value : " << m_value << endl; cout << "this : " << this << endl; } }; int main(int argc, char *argv[]) { Test test(100); //在栈空间建立对象 Test* pTest = new(&test) Test(1000); return 0; } 上述代码中,可使用new操做符的默认实如今栈空间建立对象。
new[]/delete[]关键字与new/delete关键字彻底不一样,是一组全新的关键字。
new[]关键字用于建立动态对象数组,delete[]关键字用于销毁动态对象数组。new[]/delete[]关键字能够进行重载,用于优化内存管理方式。new[]关键字返回的空间大小一般大于预期的动态数组空间大小。spa
#include <iostream> #include <string> #include <cstdlib> using namespace std; class Test { int m_value; public: Test(int value = 0) { m_value = value; } ~Test() { } void* operator new (unsigned int size) { cout << "operator new: " << size << endl; return malloc(size); } void operator delete (void* p) { cout << "operator delete: " << p << endl; free(p); } void* operator new[] (unsigned int size) { cout << "operator new[]: " << size << endl; return malloc(size); } void operator delete[] (void* p) { cout << "operator delete[]: " << p << endl; free(p); } }; int main(int argc, char *argv[]) { Test* pt = NULL; pt = new Test; delete pt; pt = new Test[5]; delete[] pt; return 0; }
上述代码中,重载了new[]/delete[]关键字。设计