Why does this return a const X * const pointer inside the const method?

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; // Error
    ...

    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
{
  // Recurse on left child without breaking if not failing
  if (this->leftChild &&!this->leftChild->IsValid(previousNode))
    return false;

  // First node retrieved - assign
  if (!*previousNode)
    *previousNode = this;
  // Previous data does not compare well to the current one - BST not valid
  else if (!Compare()((*previousNode)->data, this->data))
    return false;

  // Set current node
  *previousNode = this;

  // Recurse on right child
  if (this->rightChild && !this->rightChild->IsValid(previousNode))
    return false;

  return true;
}

. .

+4
2

:

  • a const X* X
  • a const X* const - X
  • a const X* const*

:

  • X* const* previousNode - X
  • *previousNode X
  • *previousNode = ... - . , !
+4

IsValid ( const), this , .. const X*. previousNode const X** .

+3

All Articles