#include <iostream>
template<typename T>
struct printer {
virtual const T* get(size_t& sz) const = 0;
void print() {
size_t sz;
const T* _t = get(sz); //tries to access 'this'
for (size_t t = 0; t < sz; t++)
std::cout << _t[t] << std::endl;
}
};
template<typename T, size_t sz>
struct mask_t : public printer<T> {
T data[sz];
virtual const T* get(size_t& _sz) const override {
_sz = sz;
return data;
}
};
int main(int argc, char** argv) {
char* buffer = new char[1024];
{
using mask_f = mask_t<float, 12>;
using mask_i = mask_t<int, 12>;
mask_f* mf = reinterpret_cast<mask_f*>(buffer + 42);
mask_i* mi = reinterpret_cast<mask_i*>(buffer + 42);
//'this' is uninitialized
mf->print();
mi->print();
mask_f _mf = *mf;
mask_i _mi = *mi;
//ok
_mf.print();
_mi.print();
}
delete[] buffer;
return 0;
}
print()
tries to access this
when invoking get()
is it because of a vfptr
lookup ? In other words is this impossible to do ?
Edit : I know I can create a new mask_t
with either new
or as I have done here by dereferencing the pointer. Then mask_t::this
is defined.
The reason I don't want to create the instance is for performance issues [and that's not visible in my example I admit]. If you want to answer please address the only question in this post.