博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ Primer Plus 学习笔记 第十二章 对象指针小总结下
阅读量:4127 次
发布时间:2019-05-25

本文共 3679 字,大约阅读时间需要 12 分钟。

 

如果是通过定位new运算符创建的对象 在释放内存的时候不会调用对象的析构函数

char * buffer = new char[BUF];pc1 = new (buffer) JustTesting;delete [] buffer;// 这时候是不会调用~JustTesting()的

使用定位new运算符时 要手动的空值内存位置 不然一不小心就把之前见的对象覆盖掉了

pc3 = new (buffer + sizeof(JustTesting))  JustTesting("Better Idea", 6);

正常情况下 使用定位new 运算符时  delete [] 会有释放掉内存 但是如果依然希望程序能够调用析构函数的话

就显式的调用

pc1->~JustTesting();

 

我这里测试出来一个事情:

当调用析构函数后(定位new) 指针依然能够使用 当执行delete [] 释放数组之后才会出现数据异常的情况

如果是正常的new出来的对象 调用析构函数之后 对象指针就没用了

构造函数初始化对象const常量类型的成员时不能直接赋值。 因为人家是常量。需要在程序执行到构造函数体之前进行赋值:

Queue::Queue(int qs) : qsize(qs){    front = rear = NULL;    items = 0;}

这种用法仅限于构造函数。 :号后面的成员数量可以不止一个,const和声明为引用的类成员必须用这个方法赋值。

最后那个队列模式

其实就是单链表

// queue.h#ifndef QUEUE_H_#define QUEUE_H_class Customer{  private:    long arrive;    int processtime;  public:    Customer() {arrive = processtime = 0;}    void set(long when);    long when() const {return arrive;}    int ptime() const {return processtime;}};typedef Customer Item;class Queue{  private:    struct Node {Item item; struct Node * next;};    enum {Q_SIZE = 10};    Node * front;    Node * rear;    int items;    const int qsize;    Queue(const Queue & q) : qsize(0) {}    Queue & operator=(const Queue & q) {return *this;}  public:    Queue(int qs = Q_SIZE);    ~Queue();    bool isempty() const;    bool isfull() const;    int queuecount () const;    bool enqueue(const Item &item);    bool dequeue(Item &item);};#endif
// queue.cpp#include "queue.h"#include 
Queue::Queue(int qs) : qsize(qs){ front = rear = NULL; items = 0; }Queue::~Queue(){ Node * temp; while (front != NULL) { temp = front; front = front->next; delete temp; }}bool Queue::isempty() const{ return items == 0;}bool Queue::isfull() const{ return items == qsize;}int Queue::queuecount() const{ return items;}bool Queue::enqueue(const Item & item){ if (isfull()) return false; Node * add = new Node; add -> item = item; add->next = NULL; items++; if (front == NULL) front = add; else rear -> next = add; rear = add; return true;}bool Queue::dequeue(Item & item){ if (front == NULL) return false; item = front -> item; items--; Node * temp = front; front = front -> next; delete temp; if (items == 0) rear = NULL; return true;}void Customer::set(long when){ processtime = std::rand() % 3 + 1; arrive = when;}

 

bank.cpp#include 
#include
#include
#include "queue.h"const int MIN_PER_HR = 60;bool newcustomer(double x);int main(){ using std::cin; using std::cout; using std::endl; using std::ios_base; std::srand(std::time(0)); cout << "Case Study: Bank of Heather Automatic Teller\n"; cout << "Enter maximum size of queue: "; int qs; cin >> qs; Queue line(qs); cout << "Enter the number of simulation hours: "; int hours; cin >> hours; long cyclelimit = MIN_PER_HR * hours; cout << "Enter the average number of customers per hour: "; double perhour; cin >> perhour; double min_per_cust; min_per_cust = MIN_PER_HR / perhour; Item temp; long turnaways = 0; long customers = 0; long served = 0; long sum_line = 0; int wait_time = 0; long line_wait = 0; for (int cycle = 0; cycle < cyclelimit; cycle++) { if (newcustomer(min_per_cust)) { if (line.isfull()) turnaways++; else { customers++; temp.set(cycle); temp.set(cycle); line.enqueue(temp); } } if (wait_time <= 0 && !line.isempty()) { line.dequeue(temp); wait_time = temp.ptime(); line_wait += cycle - temp.when(); served++; } if (wait_time > 0) wait_time--; sum_line += line.queuecount(); } if (customers > 0) { cout << "customers accepted: " << customers <

总结:

转载地址:http://snepi.baihongyu.com/

你可能感兴趣的文章
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(三)
查看>>
pytorch(5)
查看>>
ubuntu相关
查看>>
C++ 调用json
查看>>
nano中设置脚本开机自启动
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
手绘VS码绘(二):动态图绘制(码绘使用Processing)
查看>>
基于P5.js的“绘画系统”
查看>>