【问题标题】:Super vs Subclass inheritance of a constructor C++构造函数C ++的超级与子类继承
【发布时间】:2012-10-16 11:14:15
【问题描述】:

所以这是具有左、右、父和数据的二叉搜索树的基类。

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 的成员。我知道它应该识别它们,因为它应该继承这些信息。任何帮助都会得到帮助。

【问题讨论】:

标签: c++ inheritance constructor subclass base


【解决方案1】:

好的,我在 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 的成员。我知道它应该识别>它们,因为它应该继承这些信息。任何帮助都会得到帮助。

上面的代码实现了一个构造函数,或者如果你想专门设置left、right和parent,那么你需要:

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

然后在 RSTNode 中使用它,或者有一个类似的用于 RSTNode 传递到那个....

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

希望能有所帮助,请注意,您应该更喜欢初始化列表而不是直接访问 ctor 中的成员。但是,如果您不能更改基类,那么您需要...


更正错字 - 数据 -> 数据

【讨论】:

    【解决方案2】:

    这是因为数据成员的默认范围是private。如果要在子类中访问它们,则需要将它们声明为 protected

    更好的是,向BSTNode 添加一个构造函数,允许您传入初始化值,并从RSTNode 构造函数中调用它,因为它应该是管理其成员生命周期的基类。

    【讨论】:

      【解决方案3】:

      如果我没看错,您尝试从模板类继承:

      class RSTNode: public BSTNode<Data>
      

      您的 BSTNode 类定义在哪里不是模板类?

      class BSTNode {
      

      这是问题的一部分还是您粘贴了错误的代码?

      您可以通过模板化 BSTNode 来修复它

      template <typename T> class BSTNode {
      

      或从非模板化的 BSTNode 派生 RSTNode:

      class RSTNode: public BSTNode
      

      然而你已经尝试过你所写的这一事实意味着你并没有真正理解更深层次的类定义,而且你试图设置基类的事实直接在派生类构造函数中的参数并且您已将它们公开将使我相信您需要了解更多有关面向对象设计的信息 - 因此,尽管这可能在技术上解决您的问题,但它只是冰山一角您遇到的问题。

      另外说“由于某种原因它无法识别 BSTNode 的成员”并不是很有帮助,因为我怀疑这不是您尝试执行此操作时编译器的确切输出。

      【讨论】:

      • 我确实在我的类声明前面有这个:template&lt;typename Data&gt; 不知道为什么它没有被我的代码复制到顶部。抱歉,如果这是一个问题。
      猜你喜欢
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2018-06-06
      • 2012-01-31
      • 2017-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多