Consider the following code in C++17:
#include <bits/stdc++.h>
using namespace std;
class MyClass {
public:
int attribute;
MyClass(){
this->attribute = 1;
}
void update(int x){
this->attribute = x;
}
};
void testing(){
map<string, MyClass> mapping;
MyClass one;
mapping["hh"] = one;
cout << one.attribute << endl; //1
one.update(10);
cout << one.attribute << endl; //10
cout << mapping["hh"].attribute << endl; //1
}
int main(){
testing();
return 0;
}
Suppose I have a map with a default constructable class as values and I want to update the map by accessing value with the key and then modifying the attributes. The class is and must be updated by a method. The key should remain unchanged.
What is the best way of doing this? []
operator does not seem to work in this case.