June 1st Monday (六月 一日 月曜日)

本文介绍了一个使用模板元编程实现的简单示例,包括一个泛型栈类和一个使用该栈类作为参数的螃蟹类。此外,还展示了如何利用C++标准库中的atexit函数来注册退出处理程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Oh!  Template as parament is not supported in Visual C++ 6.0.  Maybe it is so old. ^_^

  I have to build the following codes in Visual C++ 2005.

//temparg.cpp
#include <iostream>
using namespace std;

template <class T>
class Stack {
    int cnt;
    T* bottom, *top;

public:
    Stack() {
        bottom = NULL;
        top = NULL;
        cnt = 0;

        bottom = new T[10];
        top = bottom;
    }

    ~Stack() {
        delete [] bottom;
    }

    bool push(T t) {
        (*top) = t;
        top++;
        cnt++;

        return (cnt>10) ? false : true;
    }

    bool pop(T& tr) {
        top--;
        cnt--;
        tr = (*top);
        return (cnt>=0) ? false : true;
    }
};

template <template <typename T> class Thing>
class Crab
{
private:
    Thing<int> s1;
    Thing<double> s2;
public:
    Crab() {};
    // assumes the thing class has push() and pop() members
    bool push(int a, double x) { return s1.push(a) && s2.push(x); }
    bool pop(int & a, double & x){ return s1.pop(a) && s2.pop(x); }
};

int main()
{
    Crab<Stack> nebula;
// Stack must match template <typename T> class thing
    int ni;
    double nb;

    while (cin>> ni >> nb && ni > 0 && nb > 0)
    {
        if (!nebula.push(ni, nb))
            break;
    }

    while (nebula.pop(ni, nb))
           cout << ni << ", " << nb << endl;
    cout << "Done./n";

    return 0;
}

  The example is about exit function.

//ex.c
#include <stdlib.h>

void ex1() {
  printf("exit 1/n");
}

void ex2() {
  printf("exit 2/n");
}

int main() {
  atexit(ex1);
  atexit(ex2);

  return 0;
}

$ ./ex
exit 2
exit 1

  From the above, you can know the order of calling your exit functions.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值