📄 pstash.cpp
字号:
//: C13:PStash.cpp {O}
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Pointer Stash definitions
#include "PStash.h"
#include <iostream>
#include <cstring> // Mem functions
using namespace std;
int PStash::add(void* element) {
const int inflateSize = 10;
if(next >= quantity)
inflate(inflateSize);
storage[next++] = element;
return(next - 1); // Index number
}
// Operator overloading replacement for fetch
void* PStash::operator[](int index) const {
if(index >= next || index < 0)
return 0; // Out of bounds
// Produce pointer to desired element:
return storage[index];
}
void PStash::inflate(int increase) {
const int psz = sizeof(void*);
// realloc() is cleaner than this:
void** st = new void*[quantity + increase];
memset(st, 0, (quantity + increase) * psz);
memcpy(st, storage, quantity * psz);
quantity += increase;
delete storage; // Old storage
storage = st; // Point to new memory
} ///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -