Piszę prostą implementacje tablicy asocjacyjnej opartą na liście jednokierunkowej:
#include <iostream>
#include <string>
class AssocArrayList {
friend class AssocArray;
public:
typedef int T;
AssocArrayList(std::string key);
~AssocArrayList();
private:
std::string _key;
T _value;
AssocArrayList *_next;
};
class AssocArray {
public:
typedef int T;
~AssocArray();
AssocArray();
T &operator[](std::string key) const;
AssocArray &operator=(const AssocArray& tab);
private:
mutable AssocArrayList *_list;
};
AssocArrayList::~AssocArrayList() {
if (_next) delete _next;
}
AssocArrayList::AssocArrayList(std::string key) : _key(key), _value(0), _next(0) {}
AssocArray::~AssocArray() {
if (_list) delete _list;
}
AssocArray::AssocArray() : _list(0) {}
AssocArray &AssocArray::operator=(const AssocArray& tab) {
std::cout << _list << tab._list;
_list = tab._list;
return *this;
}
AssocArray::T &AssocArray::operator[](std::string key) const {
if (_list == 0) {
_list = new AssocArrayList(key);
return _list->_value;
}
AssocArrayList *cur = _list;
AssocArrayList *prev = 0;
while(cur) {
if (cur->_key == key) {
return cur->_value;
}
prev = cur;
cur = cur->_next;
}
prev->_next = new AssocArrayList(key);
return prev->_next->_value;
}
void func(const AssocArray &tab) {
std::cout << tab["a"] << tab["b"] << tab["c"] << tab["d"] << "\n";
}
AssocArray func2() {
AssocArray tab;
tab["a"] = tab["b"] = tab["c"] = tab["d"] = 9;
return tab;
}
int main() {
AssocArray tab;
tab["a"] = 4;
tab["b"] = 5;
tab["a"] = 3;
tab["d"] = tab["c"] = 1;
std::cout << tab["a"] << tab["b"] << tab["c"] << tab["d"] << "\n";
func(tab);
func(func2());
AssocArray tab2;
tab2 = func2();
return 0;
}
Wszystko działa pięknie aż do tego fragmentu: tab2 = func2();
. Z niewiadomych mi przyczyn w tym momencie pojawia się segfault. Próbowałem przeciążyć operator = (AssocArray &AssocArray::operator=(const AssocArray& tab)
) aby mieć lepszy wgląd co się dzieje no i okazało się, że nie można skopiować wskaźnika do wskaźnika: _list = tab._list;
. Ktoś mi może wytłumaczyć dlaczego?