请问这段程序是什么意思
程序代码:
#include <iostream> class bullet { private: bool buse = true; float x; float y; float h; float damage; public: char* mem = new char[1000 * sizeof(bullet)]{}; void* operator new(size_t size) { bullet* bull_ptr = (bullet*)mem; for (int i = 0; i < 1000; i++) { if (!bull_ptr[i].buse) return &bull_ptr[i]; } } ~bullet() { buse = false; } }; int main() { bullet* bul1 = new bullet; bullet* bul2 = new bullet; bullet* bul3 = new bullet; delete bul1; bullet* bul4 = new bullet; bullet* bul5 = new bullet; std::cout << bul1 << std::endl; std::cout << bul2 << std::endl; std::cout << bul3 << std::endl; std::cout << bul4 << std::endl; std::cout << bul5 << std::endl; system("pause"); }
对于这段代码,我有两个问题
当我打印bul1和bul2的地址时,发现相隔
00FC2098
00FC20AC
20个字节,但是我的成员变量却是1+4+4+4+4=17个字节
另外,不是很理解这段代码到底干了啥,题目本意是
假设有1000个子弹,定义1000个子弹的空间
当子弹1释放后,子弹4就要放到子弹1的地址。
[此贴子已经被作者于2023-5-3 17:56编辑过]