【发布时间】:2013-03-31 16:38:36
【问题描述】:
我对 C++ 很陌生,并且认为这个问题从根本上与指针有关;进行了研究,但找不到与以下上下文相关的任何明显内容。
我已经概述了我的代码结构,以突出我遇到的问题,它试图通过指针root 访问嵌套的Node 类成员函数isLeftChild 到常量Node;我可以让isLeftChild 成为Tree 类的成员函数,但我觉得isLeftChild 成为嵌套Node 类的成员函数更合乎逻辑。
class Tree {
class Node {
public:
bool isLeftChild(void);
};
Node const* root;
public:
void traverse(Node const* root);
};
void Tree::traverse(Node const* root) {
// *** Line below gives compile error: request for member 'isLeftChild' in
// 'root', which is of non-class type 'const Tree::Node*'
if ( root.isLeftChild() ) {
cout << "[is left child]";
}
}
bool Tree::Node::isLeftChild(void){
bool hasParent = this->parent != NULL;
if ( hasParent ) {
return this == this->parent->left;
} else {
return false;
}
}
如何从traverse 成员函数中访问此成员函数?问题是否围绕root 是一个指针这一事实?
谢谢,亚历克斯
【问题讨论】:
标签: c++ class pointers tree member