I try following code
#include "iostream"
class Cls {
public:
// Assume Cls has an array-like structure
int operator[](int idx) {
// Provide access to an element
std::cout << idx << std::endl;
return 0;
}
void operator[]= (int idx, int newV) {
}
};
int main() {
// Create an instance of Cls
auto cls = Cls();
// Assign a new value to the i-th element
int i = 5; // Example index
int newV = 10; // Example new value
cls[i] = newV;
// Now cls[i] will have the value of newV
return 0;
}
void operator[]= (int idx, int newV) {
raise err ‘operator[]’ cannot be the name of a variable or data member
1