[英]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的成員。 我知道它應該識別它們,因為它應該繼承此信息。 任何幫助都適用。
這是因為數據成員的默認范圍是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的成員”並不是很有幫助,因為我懷疑這並不是編譯器的確切輸出。
好的,我在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中的成員。 但是,如果您不能更改基類,則需要...
更正的錯字-數據->數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.