Mam funkcję, która przyjmuje odniesienie const jako argument. Nie powinien zmieniać argumentu, ale robi to (zmienna "_isVertex"). Jak to może zostać naprawione? Oto kod:Zmiany funkcji const obiekt
#include <vector>
#include <iostream>
using namespace std;
class Element
{
public:
bool isVertex() const
{ return _isVertex; };
private:
bool _isVertex = true;
};
class ElementContainer : public vector <Element>
{
public:
void push(const Element &t)
{
// here everything is fine
cerr << t.isVertex() << ' ';
push_back(t);
// and here _isVertex is false, should be true!
cerr << t.isVertex() << '\n';
}
};
int main()
{
ElementContainer vertex;
vertex.push({});
vertex.push(vertex[0]);
}
Plus: masz mnie. Zauważ, że kontenery biblioteki standardowej C++ nie zostały zaprojektowane jako klasy bazowe. – Bathsheba
'const' na końcu deklaracji funkcji gwarantuje tylko, że stan klasy nie zmieni się w wyniku wywołania funkcji. Zwrócona wartość nie jest stała i może być zmieniona po fakcie. –