简体   繁体   English

派生类构造函数调用基类构造函数

[英]Derived class constructor calling base class constructor

My question is whether or not it's ok call the base class constructor as being initialized to nullptr the way I have below in derivedClassA . 我的问题是是否可以将基类构造函数初始化为nullptr ,方法与我在以下derivedClassA The code works as expected but it still feels wrong. 该代码按预期工作,但仍然感觉不对。

For the sake of length I have omitted other classes that are derived from class Tree , but the idea here is that you can create a Tree object that has any number of derived classes, and call getStringExpression() and it will return whatever getStringExpression evaluates to. 为了长度起见,我省略了从Tree类派生的其他类,但是这里的想法是,您可以创建一个具有任意数量的派生类的Tree对象,并调用getStringExpression() ,它将返回getStringExpression计算得出的结果。 For example, if an object of type Tree contains a pointer to an object of DerivedClassA , which has right and left children equal to "Car" and "Ride" then Tree.getStringExpression(); 例如,如果Tree类型的对象包含一个指向DerivedClassA对象的指针,该对象的左右子DerivedClassA分别等于“ Car”和“ Ride”,则Tree.getStringExpression(); would return "(Car + Ride)". 将返回“(汽车+乘车)”。 Assume the values Car and Ride are returned by concrete classes derived from Tree that return the string "Car" and "Ride" respectively when their getStringExpression() method is called (and they have no children). 假定值Car和Ride由Tree派生的具体类返回,当调用它们的getStringExpression()方法(并且它们没有子代)时,它们分别返回字符串“ Car”和“ Ride”。

It feels wrong to initialize the base class constructor to nullptr the way I have in derivedClassA because if I create a single object of derivedClassA, then this object is the root node with its two children created during its construction. 将基类构造函数初始化为nullptr的方式与我在namedClassA中的方式一样,这是错误的,因为如果我创建一个namedClassA的单个对象,则该对象是在其构造期间创建的带有两个子代的根节点。 What happens to the constructed Tree in this scenario? 在这种情况下,构造的Tree会发生什么? Since it's pointer is nullptr it's not really being used or it's sort of disconnected or univolved with the object of derivedClassA ... 由于它的指针是nullptr它不是真正被使用或这有点断开或univolved与对象derivedClassA ...

I apologize in advance because I know this isn't the most straightforward question. 我事先道歉,因为我知道这不是最直接的问题。

Tree.h- 树h

class Tree
{
public:
    explicit Tree(Tree *rootNode);
    virtual ~Tree() {};
    virtual std::string getStringExpression() const;
private:
    Tree *rootNodePtr = nullptr;
};

Tree.cpp- 树.cpp-

#include "Tree.h"

Tree::Tree(Tree *rootNode)
{
    rootNodePtr = rootNode;
}

std::string Tree::getStringExpression() const
{
    if(rootNodePtr != nullptr)
    {
        return rootNodePtr->getStringExpression();
    }
        return "Tree has no children";
}

derivedClassA.h- 派生的ClassA.h-

#include "Tree.h"
class derivedClassA :
    public Tree
{
public:
    derivedClassA(Tree *leftChild, Tree *rightChild);
    virtual ~derivedClassA();
    virtual std::string getStringExpression() const override;
private:
    Tree *leftChildPtr = nullptr;
    Tree *rightChildPtr = nullptr;

};

derivedClassA.cpp- 派生的ClassA.cpp-

#include "derivedClassA.h"

derivedClassA::derivedClassA(Tree *leftChild, Tree *rightChild): Tree(nullptr)
{
    leftChildPtr = leftChild;
    rightChildPtr = rightChild;

}


derivedClassA::~derivedClassA()
{
}

std::string derivedClassA::getStringExpression() const
{
    auto expressionValue = "(" + leftChildPtr->getStringExpression() + "+" + rightChildPtr->getStringExpression() + ")";
    return expressionValue;
}

It sounds like you have merged the classes of Tree and Node . 听起来您已经合并了TreeNode的类。 You are initializing Tree in the constructor with a single "root" tree, when it should have a "root Node that itself has two children. In that case, Node wouldn't inherit from Tree - you would have two separate classes. 您将在构造器中使用单个“根”树初始化Tree ,此时该树应该具有一个“根Node ,而“根Node ”本身具有两个子级。在这种情况下, Node不会从Tree继承-您将有两个单独的类。

Or perhaps you have an abstract Node class and a BinaryNode that inherits it to represnet a node with two children, opening up the door for different node types (nodes with more than two children, or unbalanced children). 或许你有一个抽象Node类和BinaryNode继承它represnet一个节点有两个孩子,对外开放的大门不同的节点类型(节点有两个以上的孩子,或不平衡儿童)。

So to answer your question, yes it seems odd that you are initializing the "base" with a default value, but I think it's because your class hierarchy is wrong. 因此,要回答您的问题,是的,使用默认值初始化“基数”似乎很奇怪,但是我认为这是因为您的类层次结构错误。 Meaning derivedClassA is not a Tree . 意思derivedClassA 不是 Tree

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

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