Skip to content
AgileLogics
4 min read

Class Templates

Prerequisites: Function templates

A class can be templated the same way a function can:

template<typename T>
class Box {
private:
    T value;

public:
    Box(T value) : value(value) {}

    T get() const {
        return value;
    }
};

Box<int> intBox(42);
Box<std::string> stringBox("Hello");

Every STL container covered next (std::vector<T>, std::map<K, V>) is itself a class template — this is exactly the mechanism that lets one vector implementation work for int, std::string, or any type you define.