简体   繁体   English

构造函数C ++的Super vs Subclass继承

[英]Super vs Subclass inheritance of a constructor C++

So this is the base class for a binary search tree with left, right, parent and data. 因此,这是具有左,右,父级和数据的二叉搜索树的基类。

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;
    }
};

The subclass is RSTNode that is supposed to use all the members of BSTNode and add a priority on top of that: 子类是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();
    }
};

Now the problem is i'm not sure how to implement the constructor for the RSTNode as it does not recognize the members of BSTNode for some reason. 现在的问题是我不确定如何为RSTNode实现构造函数,因为由于某种原因它无法识别BSTNode的成员。 I know that it should recognize them as it is supposed to inherit this information. 我知道它应该识别它们,因为它应该继承此信息。 Any help is appriciated. 任何帮助都适用。

That's because the default scope for data members is private . 这是因为数据成员的默认范围是private You need to declare them as protected if you want to access them in a subclass. 如果要在子类中访问它们,则需要将它们声明为protected

Better still, add a constructor to BSTNode that allows you to pass in initialisation values, and call this from the RSTNode constructor, since it should be the base class that is managing the lifetime of its members. 更好的是,向BSTNode添加一个构造函数,该构造函数允许您传入初始化值,并从RSTNode构造函数调用此函数,因为它应该是管理其成员生存期的基类。

If I've read this correctly your attempting to inherit from a templated class: 如果我已正确阅读此书,则您尝试从模板化类继承:

class RSTNode: public BSTNode<Data>

Where your class definition of BSTNode isn't a templated class? 您的BSTNode的类定义不是模板类的地方?

class BSTNode {

Is this part of the problem or have you pasted the wrong code? 这是问题的一部分还是您粘贴了错误的代码?

You could fix it by either templating BSTNode 您可以通过模板化BSTNode来修复它

template <typename T> class BSTNode {

or deriving RSTNode from non-templated BSTNode: 或从非模板化的BSTNode派生RSTNode:

class RSTNode: public BSTNode

However the fact that you've tried to to what you've written implies you aren't really understanding class definitions at a much deeper level, and the fact you are trying to set base class parameters directly in a derived class constructor and that you've made them public would lead me to believe you need to learn more about object oriented design - so although this might solve your problem technically, it's only tip of the iceberg when it comes to the issues you are having. 但是 ,您尝试完成所写内容的事实意味着您实际上并没有真正更深入地了解类定义,并且您试图直接在派生类构造函数中设置基类参数的事实以及您已经将它们公开,这使我相信您需要了解有关面向对象设计的更多信息-因此,尽管这可以从技术上解决您的问题,但这只是涉及到的冰山一角。

Also saying "it does not recognize the members of BSTNode for some reason" isn't very helpful as I suspect that isn't the exact output of your compiler when you try to do it. 另外说“由于某种原因它不能识别BSTNode的成员”并不是很有帮助,因为我怀疑这并不是编译器的确切输出。

Ok, I compiled this in Visual Studio... 好的,我在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;
}

It compiled so no access issues. 它编译后没有访问问题。 Like other posters above it seems to me that you are either not posting the detail of your problem, or you are not understanding something fundemental. 像上面的其他海报一样,在我看来,您要么未发布问题的详细信息,要么您不了解基本的知识。

>Now the problem is i'm not sure how to implement the constructor for the RSTNode as it >does not recognize the members of BSTNode for some reason. >现在的问题是我不确定如何为RSTNode实现构造函数,因为由于某种原因它不识别BSTNode的成员。 I know that it should recognize >them as it is supposed to inherit this information. 我知道它应该识别它们,因为它应该继承此信息。 Any help is appriciated. 任何帮助都适用。

The code above implements a constructor, or if you wanted to specifically have left, right and parent set then you would need : 上面的代码实现了一个构造函数,或者如果您想专门设置左,右和父集,则需要:

BSTNode(const Data & d, BSTNode* l, BSTNode* r, BSTNode* p) 
       : data(d),
       left(l),
       right(r),
       parent(p)
    {
    }

and then use it in the RSTNode, or have a similar one for RSTNode that passed through to that one.... 然后在RSTNode中使用它,或者为传递给该RSTNode的RSTNode使用类似的...。

RSTNode(Data const & d, BSTNode* l, BSTNode* r, BSTNode* p)
   : priority(rand()),
     BSTNode<Data>(d,l,r,p)
{
}

Hope that helps a little, note that you should prefer initialiser lists to direct access to members in the ctor. 希望对您有所帮助,请注意,您应该首选初始化程序列表来直接访问ctor中的成员。 But if you cannot change the base class then you would need to... 但是,如果您不能更改基类,则需要...


Corrected typo - data -> Data 更正的错字-数据->数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM