I'm a little new to CPP and I don't know why setValue() const works in the meanwhile as const.
Why does a class allow modification from const public
It seems really strange there is no error in g ++ -Wall or with MS Visual C ++
Here is my code:
main.cpp
#include <iostream> #include <cassert> #include "DArray.h" int main(void) { DArray darray(1); darray.setValue(0, 42); assert(darray.getValue(0) == 42); darray.~DArray(); system("pause"); return 0; }
DArray.h
class DArray { private: int* tab; public: DArray(); DArray(unsigned int n); ~DArray(); int& getValue(unsigned int n) const; void setValue(unsigned int n, int value) const; };
DArray.cpp
#include "DArray.h" DArray::DArray() { } DArray::DArray(unsigned int n) { tab = new int[n]; } DArray::~DArray() { delete[] tab; tab = nullptr; } int& DArray::getValue(unsigned n) const { return tab[n]; } void DArray::setValue(unsigned n, int value) const
c ++ c ++ 11
Automne von Einzbern
source share