笔试题 一统计对象中某个成员变量的访问次数遗失的关键字mutable 是为了突破 const 函数的限制而设计的mutable 成员变量将永远处于可改变的状态mutable 在实际的项目开发中被严谨滥用mutable 的深入分析mutable 成员变量破坏了只读对象的内部状态const 成员函数保证只读对象的状态不变性mutable 成员变量的出现无法保证状态不变性编程实验: 成员变量的访问统计mutable 的实现:#include <iostream>#include <string>using namespace std;class Test{private: int m_value; mutable int m_count; // 注意这里!public: Test(int value = 0) { m_value = value ; m_count = 0; } int getValue() const { m_count ++; return m_value; } void setValue(int value) { m_count ++; m_value = value; } int getCount() const { return m_count; }};int main(){ Test t; t.setValue(10); cout << “t.m_value = " << t.getValue() << endl; cout << “t.m_vount = " << t.getCount() << endl; const Test ct(200); cout << “ct.m_value = " << ct.getValue() << endl; cout << “ct.m_vount = " << ct.getCount() << endl; return 0;}输出:t.m_value = 10t.m_vount = 2ct.m_value = 200ct.m_vount = 1统计方式的改进:#include <iostream>#include <string>using namespace std;class Test{private: int m_value; int * const m_pCount; // 注意这里!public: Test(int value = 0) : m_pCount(new int(0)) { m_value = 0; } int getValue() const { m_pCount = m_pCount + 1; return m_value; } void setValue(int value) { m_pCount = m_pCount + 1; m_value = value; } int getCount() const { return m_pCount; }};int main(){ Test t; t.setValue(10); cout << “t.m_value = " << t.getValue() << endl; cout << “t.m_vount = " << t.getCount() << endl; const Test ct(200); cout << “ct.m_value = " << ct.getValue() << endl; cout << “ct.m_vount = " << ct.getCount() << endl; return 0;}输出:t.m_value = 10t.m_vount = 2ct.m_value = 0ct.m_vount = 1分析:int * const m_pCount; ==> 定义指针常量m_pCount = m_pCount + 1; ==> 修改指针常量指向的内存空间处的值 ==> 对象内部状态没有发生改变面试题 二new 关键字创建出来的对象位于什么地方呢?堆(默认)栈全局数据区被忽略的事实new / delete 的本质是 C++ 预定义的操作符C++ 对这两个操作符做了严格的行为定义new :获取足够大的内存空间(默认为堆空间)在获取的空间中调用构造函数创建对象delete :调用析构函数销毁对象归还对象所占用的空间(默认为堆空间)在 C++ 中能够重载 new / delete 操作符全局重载(不推荐)局部重载(针对具体类进行重载)重载 new / delete 的意义在于改变动态对象创建时的内存分布方式new / delete 的重载方式默认为静态成员函数// static member functionvoid operator new(unsigned int size){ void ret = NULL; / ret point to allocated memory / return ret;}// static member functionvoid operator delete(void p){ /* free the memory which is pointed by p /}编程实验: 静态存储区中创建对象#include <iostream>#include <string>using namespace std;class Test{private: static const unsigned int COUNT = 4; static unsigned char c_buffer[]; static unsigned char c_map[]; int m_value;public: 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; } void operator delete (void* p) { if( p != NULL ) { unsigned char* mem = reinterpret_cast<unsigned 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; } } }};unsigned char Test::c_buffer[Test::COUNT] = {0};unsigned char Test::c_map[Test::COUNT] = {0};int main(){ 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;}输出:===== Test Single Object ====Succeed to allocate memory: 0x804a0d4succeed to free memory: 0x804a0d4==== Test Object Array ====Succeed to allocate memory: 0x804a0d4pa[0] = 0x804a0d4Succeed to allocate memory: 0x804a0d8pa[1] = 0x804a0d8Succeed to allocate memory: 0x804a0dcpa[2] = 0x804a0dcSucceed to allocate memory: 0x804a0e0pa[3] = 0x804a0e0pa[4] = 0delete 0x804a0d4succeed to free memory: 0x804a0d4delete 0x804a0d8succeed to free memory: 0x804a0d8delete 0x804a0dcsucceed to free memory: 0x804a0dcdelete 0x804a0e0succeed to free memory: 0x804a0e0delete 0如何在指定的地址上创建C++对象?解决方案在类中重载 new / delete 操作符在 new 的操作符重载函数中返回指定的地址在 delete 操作符重载中标记对应的地址可用编程实验: 自定义动态对象的存储空间#include <iostream>#include <string>#include <cstdlib>using namespace std;class Test{private: static unsigned int c_count; static unsigned char* c_buffer; static unsigned char* c_map; int m_value;public: static bool SetMemorySource(unsigned char* memory, unsigned int size) { bool ret = false; c_count = size / sizeof(Test); ret = (c_count && (c_map = reinterpret_cast<unsigned char*>(calloc(c_count, sizeof(unsigned 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 ) { unsigned char* mem = reinterpret_cast<unsigned 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 = NULL;unsigned char* Test::c_buffer = NULL;unsigned char* Test::c_map = NULL;int main(){ unsigned 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;}输出:===== Test Single Object ====Succeed to allocate memory: 0xbfbbacb0succeed to free memory: 0xbfbbacb0==== Test Object Array ====Succeed to allocate memory: 0xbfbbacb0pa[0] = 0xbfbbacb0Succeed to allocate memory: 0xbfbbacb4pa[1] = 0xbfbbacb4Succeed to allocate memory: 0xbfbbacb8pa[2] = 0xbfbbacb8pa[3] = 0pa[4] = 0delete 0xbfbbacb0succeed to free memory: 0xbfbbacb0delete 0xbfbbacb4succeed to free memory: 0xbfbbacb4delete 0xbfbbacb8succeed to free memory: 0xbfbbacb8delete 0delete 0被忽略的事实new[] / delete[] 与 new / delete 完全不同动态对象数组创建通过 new[] 完成动态对象数组的销毁通过 delete[] 完成new[] / delete[] 能够被重载,进而改变内存管理方式new[] / delete[] 的重载方式默认为静态成员函数// static member functionvoid* operator new[] (unsigned int size){ return malloc(size);}// static member functionvoid operator delete[] (void* p){ free(p);}注意事项new[] 实际需要返回的内存空间可能比期望的多对象数组占用的内存中需要保存数组信息(数组长度)数组信息用于确定构造函数和析构函数的调用次数编程实验: 动态数组的内存管理#include <iostream>#include <string>#include <cstdlib>using namespace std;class Test{private: int m_value;public: Test() { m_value = 0; } ~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(){ Test* pt = NULL; pt = new Test; delete pt; pt = new Test[5]; delete[] pt; return 0;}输出:operator new: 4operator delete: 0x9e3c008operator new[]: 24 ;注意这里!operator delete[]: 0x9e3c018小结new / delete 的本质为操作符可以通过全局函数重载 new / delete (不推荐)可以针对具体的类重载 new / deletenew[] / delete[] 与 new / delete 完全不同new[] / delete[] 也是可以被重载的操作符new[] 返回的内存空间可能比期望的要多以上内容参考狄泰软件学院系列课程,请大家保护原创!