The most obvious approach is to combine common bits into a base class, for example:
class TextBase { public: char* text; int n; TextBase(char* text, int N): text(text), n(N) {} void copy(const TextBase &t) { strncpy(this->text, t.text, n - 1); this->text[n - 1] = '\0'; } }; template<int N> class Text: public TextBase { public: Text(): TextBase(_buf, N) { } char _buf[N]; };
It handles the size of an object to potentially improve code size. This should be obvious because this is the first thing that comes to my mind while still waking up. Instead of moving in terms of a base that takes a parameter in the form of an erase, it avoids the need for additional storage, for example. (this approach came to my mind when I was a little distracted from awakening):
template<int N> class Text { public: Text() { } char _buf[N]; operator char const*() const { return this->_buf; } void copy(char const* source) { strncpy(this->_buf, source, N - 1); this->_buf[N - 1] = '\0'; } };
source share