Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

Whats the syntax / semantics for a “class template”?

semantics syntax
0
Posted

Whats the syntax / semantics for a “class template”?

0

Consider a container class Array that acts like an array of integers: // This would go into a header file such as “Array.h” class Array { public: Array(int len=10) : len_(len), data_(new int[len]) { } ~Array() { delete [] data_; } int len() const { return len_; } const int& operator[](int i) const { return data_[check(i)]; } int& operator[](int i) { return data_[check(i)]; } Array(const Array&); Array& operator= (const Array&); private: int len_; int* data_; int check(int i) const { if (i < 0 || i >= len_) throw BoundsViol(“Array”, i, len_); return i; } }; Just as with swap() above, repeating the above over and over for Array of float, of char, of String, of Array-of-String, etc, will become tedious. // This would go into a header file such as “Array.h” template class Array { public: Array(int len=10) : len_(len), data_(new T[len]) { } ~Array() { delete [] data_; } int len() const { return len_; } const T& operator[](int i) const { return data_[check(i)]; } T& operator[](int i

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123