现在的位置: 首页 > 综合 > 正文

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

2013年11月13日 ⁄ 综合 ⁄ 共 1347字 ⁄ 字号 评论关闭

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.

抱歉!评论已关闭.