I'm designing a class and had some concerns about const-correctness, but I don't think I have a complete understanding of what the notion truly means. If I store a raw pointer to a non-const type and want to provide const and non-const accessors to that pointer, will that break const-correctness?
class A
{
public:
void SetCount(int* pCount)
{
m_pCount = pCount;
}
void GetCount(const int** ppCount) const
{
*ppCount = m_pCount;
}
void GetCount(int** ppCount) const
{
*ppCount = m_pCount;
}
private:
int* m_pCount = nullptr;
};
int main()
{
int count = 10;
A a;
a.SetCount(&count);
const int* pCountConst = nullptr;
a.GetCount(&pCountConst);
// Prints 10.
std::cout << *pCountConst << std::endl;
int* pCountNonConst = nullptr;
a.GetCount(&pCountNonConst);
(*pCountNonConst)++;
// Prints 11.
std::cout << *pCountConst << std::endl;
}
pCount's type is const int*, so we expect that the underlying int will never change once initialized. It seems to me that we're breaking const-correctness here, is that true?