构造函数C 的超级与子类继承
Super vs Subclass inheritance of a constructor C++
,所以这是带有左,右,父和数据的二进制搜索树的基类。
template<class Data>
class BSTNode
{
public:
/** Constructor. Initialize a BSTNode with the given Data item,
* no parent, and no children.
*/
BSTNode(const Data & d) : data(d)
{
left = right = parent = 0;
}
BSTNode<Data>* left;
BSTNode<Data>* right;
BSTNode<Data>* parent;
Data const data; // the const Data in this node.
/** Return the successor of this BSTNode in a BST, or 0 if none.
** PRECONDITION: this BSTNode is a node in a BST.
** POSTCONDITION: the BST is unchanged.
** RETURNS: the BSTNode that is the successor of this BSTNode,
** or 0 if there is none.
*/
BSTNode<Data>* successor()
{
BSTNode<Data>* cursor;
BSTNode<Data>* par;
cursor = this->right;
par = this->parent;
if (this->right != NULL)
{
while (cursor->left != NULL) {
cursor = cursor->left;
}
return cursor;
}
if ((this->right == NULL) && (this == par->left))
return this->parent;
if ((this->right == NULL) && (this == par->right))
{
do
{
cursor = par;
par = par->parent;
if (par == NULL)
{return cursor;}
} while(cursor != par->left);
return par;
}
if (this->right == NULL && this->parent == NULL)
return NULL;
return NULL;
}
};
子类是rstnode,应该使用BSTNODE的所有成员,并在此上添加优先级:
template<class Data>
class RSTNode: public BSTNode<Data>
{
public:
int priority;
RSTNode(Data const & d)
: BSTNode<Data>(d)
{
//call a random number generator to generate a random priority
priority = rand();
}
};
现在问题是我不确定如何实现rstNode的构造函数,因为它由于某种原因无法识别BSTNODE的成员。我知道它应该承认它们,因为它应该继承这些信息。任何帮助都会被化为
好吧,我在Visual Studio中编译了此内容...
template<class Data>
class BSTNode
{
public:
/** Constructor. Initialize a BSTNode with the given Data item,
* no parent, and no children.
*/
BSTNode(const Data & d) : data(d)
{
left = right = parent = 0;
}
BSTNode<Data>* left;
BSTNode<Data>* right;
BSTNode<Data>* parent;
Data const data; // the const Data in this node.
};
template<class Data>
class RSTNode : public BSTNode<Data>
{
public:
int priority;
RSTNode(Data const & d)
: priority(rand()),
BSTNode<Data>(d)
{
left = 0; //Accessible because public
right = 0;
parent = 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
RSTNode<std::string> node(std::string("test"));
return 0;
}
它编译了,因此没有访问问题。像上面的其他海报一样,在我看来,您要么不发布问题的细节,要么不了解一些资金。
>现在问题是我不确定如何实现rstnode的构造函数,因为它>由于某种原因无法识别BSTNODE的成员。我知道它应该识别>它们,因为它应该继承此信息。任何帮助都会被批准。
上面的代码实现了构造函数,或者如果您想特别左,右和父集,则需要:
BSTNode(const Data & d, BSTNode* l, BSTNode* r, BSTNode* p)
: data(d),
left(l),
right(r),
parent(p)
{
}
然后在rstnode中使用它,或者对传递到该的rstNode具有类似的rstNode ....
RSTNode(Data const & d, BSTNode* l, BSTNode* r, BSTNode* p)
: priority(rand()),
BSTNode<Data>(d,l,r,p)
{
}
希望有所帮助,请注意,您应该更喜欢初始分散列表,而不是直接访问CTOR中的成员。但是,如果您不能更改基类,那么您需要...
更正的错字 - 数据 ->数据
那是因为数据成员的默认范围为 private
。如果要在子类中访问它们,则需要将它们声明为protected
。
最好还是在BSTNode
中添加一个构造函数,该构造函数允许您传递初始化值,并从RSTNode
构造函数中调用它,因为它应该是管理其成员寿命的基类。
如果我已经正确阅读了此书,则尝试从模板类继承:
class RSTNode: public BSTNode<Data>
您的bstnode的类定义不是模板的类别?
class BSTNode {
这是问题的一部分还是您粘贴了错误的代码?
您可以通过模板bstnode
来修复它template <typename T> class BSTNode {
或从非策略的bstnode派生rstNode:
class RSTNode: public BSTNode
但是您试图撰写的事实意味着您并没有真正了解班级定义,而您正在尝试设定基类的事实直接在派生的类构造函数中的参数,并且您已将它们公开会导致我相信您需要了解有关面向对象的设计的更多信息 - 因此,尽管从技术上讲这可能会解决您的问题,但在涉及到冰山的技巧只是您正在遇到的问题。
也说:"出于某种原因,它没有识别Bstnode的成员"并不是很有帮助,因为我怀疑当您尝试这样做时不是编译器的确切输出。
- 继承期间显示未知行为的子类
- C++ 继承:将子类传递给需要基类的函数并获取子类行为
- 为什么我的子类不继承父类的字符串?
- 初始化依赖于子类的继承类的常量类成员
- 当子类需要在 c++ 中相互包含时,继承有缺陷
- 无法将数据从父类继承到C++中的子类
- C++子类继承
- C++继承,如何在基类的方法中调用子类的方法?
- 如果不手动完成,子类是否继承父类的析构函数?
- 模板化类继承的子类定义中的错误
- 模板类如何在 GCC 和 Clang 中继承子类
- C 模板继承.子类应用固定类型替换基类中的类型
- 具有菱形继承结构的子类的大小似乎很奇怪
- 子类未能继承超类字段
- 构造函数C 的超级与子类继承
- 子类 B 继承自模板类 A<B>
- 从其他子类继承子类
- 子类继承的非公共虚函数会发生什么情况
- 无法使用QTableView查看所有列,我的子类继承自QAbstractTableModel。
- 使子类继承基类的现有实例