stl - in C++, how do you code a class ADT--like std::vector or std::stack--that takes a data type in <> as argument and constructs the object accordingly? -
ie. want code class adt can used this:
myadt <type> objecta;
in same way use vector like:
vector <type> avector;
i'm guessing maybe has templates , overloading <> operator, how take data type argument?
let's have class implements adt using hard-coded typedef t
:
class adt { public: typedef int t; adt(); t& operator[](size_t index); const t& operator[](size_t index) const; size_t size() const; ... private: t* p_; size_t size_; };
(you have work out internal storage , member functions appropriate actual adt).
to change can specify t
done std::vector
etc.:
template <typename t> <-- specification of t moved here class adt { public: <-- typedef t removed here adt(); t& operator[](size_t index); const t& operator[](size_t index) const; size_t size() const; ... private: t* p_; size_t size_; };
usage then:
adt<int> an_adt_int; adt<double> an_adt_double;
Comments
Post a Comment