简体   繁体   English

c ++继承和运算符重载

[英]c++ inheritance and operator overloading

I have two classes, one is the parent of the other. 我有两个班,一个是另一个班的父母。 They both need to have different operator<< functions. 它们都需要具有不同的运算符<<函数。 Each class has an output function that's unique to it. 每个类都有一个独特的输出函数。 Here's how they are more or less set up: 以下是它们或多或少的设置方式:

template <class T>
class Tree {

    protected:
        T elem;
        vector< Tree<T>* > children;

    public:
        ostream &output(ostream& stream,int level) {
            stream << "Tree" << endl;
            for (int j=0;j<level;j++) stream << "    ";
            stream << '\'' << this->elem << '\'' << endl; 
            for (unsigned int i=0;i<this->children.size();i++) {
                this->children[i]->output(stream,level+1);
            }
            return stream;
        }

        template <class U>
        friend ostream &operator<<(ostream &cout,Tree<U> &obj);

};

template <class T>
ostream &operator<<(ostream &stream,Tree<T> &obj) {
    obj.output(stream,0);
    return stream;
};

template <class T>
class ParseTree : public Tree<T> {

    protected:
        ParseTree<T>* elemTree;
        vector< ParseTree<T>* > children;

    public:
        ostream &output(ostream& stream,int level) {
            stream << "ParseTree" << endl;
            if (elemTree == NULL) {
                for (int j=0;j<level;j++) stream << "    ";
                stream << '\'' << this->elem << '\'' << endl; 
            }
            else {
                elemTree->output(stream,level+1);
            }
            for (unsigned int i=0;i<this->children.size();i++) {
                this->children[i]->output(stream,level+1);
            }
            return stream;
        }

        template <class U>
        friend ostream &operator<<(ostream &cout,ParseTree<U> &obj);
};

template <class T>
ostream &operator<<(ostream &stream,ParseTree<T> &obj) {
    obj.output(stream,0);
    return stream;
};

Both output functions recursively print out the tree, but ParseTree's is slightly different. 两个输出函数以递归方式打印出树,但ParseTree略有不同。 The problem I'm having is that when I try to cout << a ParseTree the first iteration is from ParseTree's output function (as confirmed by the stream << "ParseTree" << endl statement), but all subsequent requests seem to be Tree's ouput function (as confirmed by the stream << "Tree" << endl statement). 我遇到的问题是,当我尝试cout <<一个ParseTree时,第一次迭代来自ParseTree的输出函数(由流<<“ParseTree”<< << endl语句确认),但所有后续请求似乎都是Tree的输出函数(由流<<“Tree”<< endl语句确认)。 Every object pushed onto the children vector is definitely a ParseTree. 推送到子矢量上的每个对象绝对是ParseTree。 My guess is that ParseTree::children is different from Tree::children, and for some reason the context is getting switched. 我的猜测是ParseTree :: children与Tree :: children不同,并且由于某种原因,上下文正在切换。 Any ideas? 有任何想法吗?

Make output virtual and define operator<< only once with an argument of type Tree<T> . 使用Tree<T>类型的参数只输出virtual并定义operator<<一次。 That should be sufficient. 这应该足够了。

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

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