我正在学习c ++,我试着知道=在调用operator之后是否可以使用赋值运算符[]
我有一个包含私有int *_content数据的数组类,并且运算符已=重载.他的构造函数_content通过参数传递大小来分配我.
我的班级看起来像:
class Array {
private:
int *_content;
public:
Array(unsigned int n) {
this->_content = new int[n];
}
~Array() {
delete[] _content;
}
int operator[](int n) const {
return this->_content[n];
}
};
Run Code Online (Sandbox Code Playgroud)
我可以写下面的代码:
int main() {
Array a(10);
std::cout << a[5] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
0
Run Code Online (Sandbox Code Playgroud)
我想知道n在使用之后是否有可能由任何运营商分配我的内容[]:
a[5] = 2000;
Run Code Online (Sandbox Code Playgroud)
并且知道在这种情况下我的情况是否有任何清洁的解决方案.谢谢你的回复