0
template <typename T, size_t S> struct SArray {
  T data[S];
  size_t size;
  size_t capacity;

  T& operator[] (const int idx) {
    assert(idx >= 0 && idx < size && idx < capacity);
    return data[idx];
  }

};

template <size_t S> struct B_Array : SArray<uint8_t, S> {

  B_Array() {
    this->size = this->capacity = 0;
  }

  template <typename T> void Put(T& obj, size_t idx=0) {
    auto base = (uint8_t*)&obj;
    std::copy(base, base+sizeof(T), this->data + idx);
  }

  template <typename T> void Get(T& obj, size_t idx=0) {
    auto base = this->data + idx;
    std::copy(base, base+sizeof(T), (uint8_t*)&obj);
  }

};

In the code above, I have to refer to the size, capacity, and data values inside B_Array through the this pointer (e.g. this->size etc.), as shown above, as opposed to just size/capacity/data, even though the class is inherited. I'm sure this has something to do with template mechanics, but I'd really like to understand why.

This is CLANG in xcode, the dialect is GNU++14, if it matters.

Andrew Voelkel
  • 353
  • 2
  • 8

0 Answers0