使用自写的 Buffer 类时出现访问冲突错误:
template<typename T>
class Buffer {
public:
Buffer(T *data, size_t len);
Buffer(size_t len);
size_t len();
operator T*();
T& operator[] (const int x) const {
return this->data[x];
};
private:
T *data;
size_t _len;
};
int main() {
Buffer<char> b("123", 3);
b[0] = 0; // This line causes "Access violation writing location 0x003c8830".
return 0;
}
这是为什么?我究竟做错了什么?