-
StackOverflow 文件
-
C++ 教程
-
常量類成員函式
-
常數成員函式
#include <iostream>
#include <map>
#include <string>
using namespace std;
class A {
public:
map<string, string> * mapOfStrings;
public:
A() {
mapOfStrings = new map<string, string>();
}
void insertEntry(string const & key, string const & value) const {
(*mapOfStrings)[key] = value; // This works? Yes it does.
delete mapOfStrings; // This also works
mapOfStrings = new map<string, string>(); // This * does * not work
}
void refresh() {
delete mapOfStrings;
mapOfStrings = new map<string, string>(); // Works as refresh is non const function
}
void getEntry(string const & key) const {
cout << mapOfStrings->at(key);
}
};
int main(int argc, char* argv[]) {
A var;
var.insertEntry("abc", "abcValue");
var.getEntry("abc");
getchar();
return 0;
}