C++编程思想 第1卷 第6章 初始化和清除 带有构造函数和析构函数的Stack

带有构造和析构函数的链表
new和delete指针

 

//: C06:Stack3.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// With constructors/destructors
#ifndef STACK3_H
#define STACK3_H

class Stack {
  struct Link {
    void* data;
    Link* next;
    Link(void* dat, Link* nxt);
    ~Link();
  }* head;
public:
  Stack();
  ~Stack();
  void push(void* dat);
  void* peek();
  void* pop();
};
#endif // STACK3_H ///:~

 

Stack有构造和析构,被嵌套的 Link类也有

 

//: C06:Stack3.cpp {O}
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Constructors/destructors
#include "Stack3.h"
#include "../require.h"
using namespace std;

Stack::Link::Link(void* dat, Link* nxt) {
  data = dat;
  next = nxt;
}

Stack::Link::~Link() { }

Stack::Stack() { head = 0; }

void Stack::push(void* dat) {
  head = new Link(dat,head);
}

void* Stack::peek() { 
  require(head != 0, "Stack empty");
  return head->data; 
}

void* Stack::pop() {
  if(head == 0) return 0;
  void* result = head->data;
  Link* oldHead = head;
  head = head->next;
  delete oldHead;
  return result;
}

Stack::~Stack() {
  require(head == 0, "Stack not empty");
} ///:~

 

Stack::Link::Link简单初始化data指针和next指针

Link的析构函数不做任何事情,不删除data指针
分配和清除Link对象的实现隐藏在类Stack中,是内部实现的一部分

 

//: C06:Stack3Test.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
//{L} Stack3
//{T} Stack3Test.cpp
// Constructors/destructors
#include "Stack3.h"
#include "../require.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main(int argc, char* argv[]) {
  requireArgs(argc, 1); // File name is argument
  ifstream in(argv[1]);
  assure(in, argv[1]);
  Stack textlines;
  string line;
  // Read file and store lines in the stack:
  while(getline(in, line))
    textlines.push(new string(line));
  // Pop the lines from the stack and print them:
  string* s;
  while((s = (string*)textlines.pop()) != 0) {
    cout << *s << endl;
    delete s; 
  getchar();
  }
} ///:~

 

textlines中所有行被弹出和删除,剩下最后一行

命令行参数是 Stack3Test.cpp文件的绝对路径 
输出
} ///:~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是关于stack复制构造函数的介绍: 在C++中,复制构造函数是一种特殊的构造函数,它在创建对象时使用另一个相同类型的对象来初始化新对象。当我们使用一个对象去初始化另一个对象时,复制构造函数就会被调用。对于stack来说,复制构造函数用于创建一个新的stack对象,并将其初始化为另一个stack对象的副本。 下面是一个示例代码,展示了如何定义一个stack的复制构造函数: ```c++ #include <iostream> #include <stack> using namespace std; class MyStack { private: int* arr; int top; int capacity; public: // 默认构造函数 MyStack(int size = 10) { arr = new int[size]; capacity = size; top = -1; } // 复制构造函数 MyStack(const MyStack& other) { capacity = other.capacity; top = other.top; arr = new int[capacity]; for (int i = 0; i <= top; i++) { arr[i] = other.arr[i]; } } // 函数 ~MyStack() { delete[] arr; } // 入栈 void push(int x) { if (top == capacity - 1) { cout << "Stack Overflow" << endl; return; } arr[++top] = x; } // 出栈 void pop() { if (top == -1) { cout << "Stack Underflow" << endl; return; } top--; } // 返回栈顶元素 int peek() { if (top == -1) { cout << "Stack is Empty" << endl; return -1; } return arr[top]; } // 判断栈是否为空 bool isEmpty() { return top == -1; } // 返回栈的大小 int size() { return top + 1; } // 打印栈中的元素 void printStack() { if (top == -1) { cout << "Stack is Empty" << endl; return; } for (int i = top; i >= 0; i--) { cout << arr[i] << " "; } cout << endl; } }; int main() { MyStack s1(5); s1.push(1); s1.push(2); s1.push(3); s1.push(4); s1.push(5); MyStack s2 = s1; // 复制构造函数被调用 s1.pop(); s2.pop(); cout << "s1: "; s1.printStack(); cout << "s2: "; s2.printStack(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值