📄 mem.cpp
字号:
//: C07:Mem.cpp {O}
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
#include "Mem.h"
#include <cstring>
Mem::Mem() //The default constructor doesn’t allocate any storage
{
mem = 0; size = 0; }
Mem::Mem(int sz) //the second constructor ensures that there is sz storage in the Mem object.
{
mem = 0;
size = 0;
ensureMinSize(sz);
}
//当两个函数利用缺省参数合二为一时,代码不需要修改,这种情况适宜使用缺省参数
/*
Mem::Mem(int sz)
{
mem = 0;
size = 0;
ensureMinSize(sz);
}
*/
Mem::~Mem() //The destructor releases the storage,
{
delete []mem; }
int Mem::msize() //tells you how many bytes there are currently in the Mem object,
{
return size; }
void Mem::ensureMinSize(int minSize) //to increase the size of the memory block
{
if(size < minSize) {
byte* newmem = new byte[minSize];
memset(newmem + size, 0, minSize - size); //the first argument Pointer to destination,the second argument is Character to set,the third argument is Number of characters to be set.
memcpy(newmem, mem, size);
delete []mem;
mem = newmem;
size = minSize;
}
}
byte* Mem::pointer() //produces a pointer to the starting address of the storage
{
return mem; }
byte* Mem::pointer(int minSize) //to want a pointer to a block of bytes that is at least minSize large,
{
ensureMinSize(minSize);
return mem;
} ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -