gpt4 book ai didi

C++放置 'const'麻烦。

转载 作者:行者123 更新时间:2023-11-30 01:56:10 33 4
gpt4 key购买 nike

首先,我想提前感谢所有回答这个问题的人。非常感谢您的帮助。这是我第一次在这里发帖,所以如果我发帖不礼貌,请原谅我。

我的问题是关于方法原型(prototype)的:

void copySubtree(Node<T> * & target, Node<T> * const & original);

当我稍后在 combineTrees() 中调用 copySubtree() 时。按照目前的代码,它会构建。但我最初拥有的是:

void copySubtree(Node<T> * & target, const Node<T> * & original);

这给了我错误:

error C2664: 'RootedBinaryTree<T>::copySubtree' : cannot convert parameter 2 from 'RootedBinaryTree<T>::Node<T> *const ' to 'const RootedBinaryTree<T>::Node<T> *&'

我知道当你把 const 放在参数中的数据类型之前时,它会阻止你在你的方法中修改所述参数,但我不知道当你把它放在数据后面时它会做什么类型,我也不确定我的代码构建在数据类型之后放置 const 不仅仅是侥幸。在数据类型之后放置 const 有什么作用?按照目前的编写方式,我的代码是否会出现可怕的运行时问题?

[另外:我正在尝试编写有根二叉树模板类方法定义(这就是为什么有些方法是空的,并且在评论中有一些随机注释给自己)。因此,对于由此造成的任何不便,我深表歉意。]

相关代码如下:

RootedBinaryTree.h

#ifndef ROOTEDBINARYTREE_H
#define ROOTEDBINARYTREE_H

template <class T>
class RootedBinaryTree
{
private:
template <class T>
struct Node
{
T nodeData;
Node<T> * leftChild;
Node<T> * rightChild;
};
Node<T> * root;
Node<T> * currentPosition;

void copySubtree(Node<T> * & target, Node<T> * const & original);
void deleteSubtree(Node<T> * n);

public:
RootedBinaryTree(const T & rootData);
RootedBinaryTree(const RootedBinaryTree<T> & original);
~RootedBinaryTree();
void toRoot();
bool moveLeft();
bool moveRight();
T getData() const {return currentPosition->nodeData;};
RootedBinaryTree<T> & operator=(const RootedBinaryTree<T> & RHS);
void combineTrees(const RootedBinaryTree<T> & leftTree, const RootedBinaryTree<T> & rightTree);
void setNodeData(const T & nodeData);
};

#endif

RootedBinaryTree.cpp

#ifndef ROOTEDBINARYTREE_CPP
#define ROOTEDBINARYTREE_CPP

#include "RootedBinaryTree.h"

template<class T>
void RootedBinaryTree<T>::copySubtree(Node<T> * & target, Node<T> * const & original)
{
// later add something here to delete a subtree if the node we are trying to assign to has children
// perhaps a deleteSubtree() method

target = new Node<T>;
if(original->leftChild != 0L)
{
copySubtree(target->leftChild, original->leftChild);
}
else
{
target->leftChild = 0L;
}
// ^^^ copy targets left (and right) children to originals
if(original->rightChild != 0L)
{
copySubtree(target->rightChild, original->rightChild);
}
else
{
target->rightChild = 0L;
}
target->nodeData = original->nodeData;

}

template <class T>
void RootedBinaryTree<T>::deleteSubtree(Node<T> * n) // Done
{// Assumes that n is a valid node.
if(n->leftChild != 0L) deleteSubtree(n->leftChild); // Delete all nodes in left subtree
if(n->rightChild != 0L) deleteSubtree(n->rightChild); // Delete all nodes in right subtree
delete n;
}

template <class T>
RootedBinaryTree<T>::RootedBinaryTree(const T & rootData) // Done
{
root = new Node <T>;
root->leftChild = 0L;
root->rightChild = 0L;
root->nodeData = rootData;
currentPosition = root;
}

template <class T>
RootedBinaryTree<T>::RootedBinaryTree(const RootedBinaryTree<T> & original)
{

}

template <class T>
RootedBinaryTree<T>::~RootedBinaryTree()
{
deleteSubtree(root); // root will be valid because of our constructor and other methods
root = currentPosition = 0L;
}

template <class T>
void RootedBinaryTree<T>::toRoot() // Done
{
currentPosition = root;
}

template <class T>
bool RootedBinaryTree<T>::moveLeft() // Done
{
if(currentPosition->leftChild == 0L) return false;
currentPosition = currentPosition->leftChild;
return true;
}

template <class T>
bool RootedBinaryTree<T>::moveRight() // Done
{
if(currentPosition->rightChild == 0L) return false;
currentPosition = currentPosition->rightChild;
return true;
}

template <class T>
RootedBinaryTree<T> & RootedBinaryTree<T>::operator=(const RootedBinaryTree<T> & RHS)
{

}

template <class T>
void RootedBinaryTree<T>::combineTrees(const RootedBinaryTree<T> & leftTree, const RootedBinaryTree<T> & rightTree)
{ // Copies leftTree into root's left tree and rightTree into root's right tree.
if(root->leftChild != 0L) deleteSubtree(root->leftChild);
if(root->rightChild != 0L) deleteSubtree(root->rightChild);
copySubtree(root->leftChild, leftTree.root);
copySubtree(root->rightChild, rightTree.root);
}

template <class T>
void RootedBinaryTree<T>::setNodeData(const T & nodeData)
{
currentPosition->nodeData = nodeData;
}

#endif

再次感谢!

最佳答案

规则是 const 把自己附加到它左边的东西上,如果左边什么都没有,它就把自己附加到右边的东西上。所以在这种情况下,我们有:

const Node *p; // p is a pointer to a const Node
Node const *p; // same again, p is a pointer to a const Node
Node * const p; // p is a const pointer to a (mutable) Node
const Node * const p; // p is a const pointer to a const Node
Node const * const p; // same again, p is a const pointer to a const Node

大多数人写const Type,因为我们倾向于这样想,但有些人更喜欢写Type const,因为这个规则。

关于C++放置 'const'麻烦。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20040954/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com