I am having problems with the following error:
Error 1 error C2440: '=': it is impossible to convert from 'const X <...> * const' to 'X <...> * const'
I am trying to use a pointer to a const pointer to track a Node while traversing a tree:
bool IsValid() const
{
X* const* previousNode = new X*;
return this->IsValid(previousNode);
}
bool IsValid(X* const* previousNode) const
{
...
if (!*previousNode)
*previousNode = this;
...
return true;
}
Why is this type const X * const and cannot be used as * Const *?
Since the method is a const method, I understand that it protects the object itself from modification. Then why is the compiler trying to ensure that the pointer to the pointer returning this is constant?
I could not find the answer to this question using the search engine.
Thank you for your responses.
, , -:
bool IsValid() const
{
std::unique_ptr<const BST*> previousNode = std::unique_ptr<const BST*>(new const BST*);
*previousNode = nullptr;
return this->IsValid(prevNode);
}
bool IsValid(std::unique_ptr<const BST*>& previousNode) const
{
if (this->leftChild &&!this->leftChild->IsValid(previousNode))
return false;
if (!*previousNode)
*previousNode = this;
else if (!Compare()((*previousNode)->data, this->data))
return false;
*previousNode = this;
if (this->rightChild && !this->rightChild->IsValid(previousNode))
return false;
return true;
}
.
.