Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CPlusPlusNotesForProfessionals.pdf
Скачиваний:
47
Добавлен:
20.05.2023
Размер:
5.11 Mб
Скачать

Chapter 42: Constant class member functions

Section 42.1: constant member function

#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;

}

GoalKicker.com – C++ Notes for Professionals

235