C ++放置'const'麻烦。

时间:2013-11-18 05:48:32

标签: c++ templates const parameter-passing pass-by-reference

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

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

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

当我稍后在copySubtree()致电combineTrees()时。正如代码目前一样,它构建。但我最初的原因是:

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

再次感谢!

1 个答案:

答案 0 :(得分:3)

规则是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因为这个规则。