C++中的模板(template)

编译器对声明只做一件事情:记录下来

编译器一个时间只能够编译一个单元。

template <class T>//把函数做成模板
void swap(T& x,T& y){
    T temp = x;
    x = y;
    y = temp;
}

template <class T>
class Vector{
public:
    Vector(int);
    ~Vector();
    Vector(const Vector&);
    Vector& operator = (const Vector&);
    T& operator[](int);

private:
    T* m_elements;
    int m_size;
};

模板的用法:

template <class T,int bounds = 100>
class FixedVector{
public:
    FixedVector();
    T& operator[](int);
private:
    T elements[bounds];//fixed size array
};

举例:

//具体的举例
template<class A>
class Derived:public Base{};

template <class A>
class Derived:public List<A>{};

class SupervisorGroup:public List <Employee*>{}

猜你喜欢

转载自blog.csdn.net/hello_leiyuanyi/article/details/80593307