【问题标题】:values are getting added to base class instead of Derived Class object值被添加到基类而不是派生类对象
【发布时间】:2021-09-21 16:42:39
【问题描述】:

我正在尝试覆盖方法,但我没有找到我所做的错误。
以下是我尝试过的代码。
'#pragma 一次 #包括 使用命名空间标准;

            class Queue {
                int size;
                int* queue;
            
            public:
                Queue() {
                    size = 0;
                    queue = new int[100];
                }
                void add(int data) {
                    queue[size] = data;
                    size++;
                }
                void print() {
                    if (size == 0) {
                        cout << "Queue is empty" << endl;
                        return;
                    }
                    for (int i = 0; i < size; i++) {
                        cout << queue[i] << " <- ";
                    }
                    cout << endl;
                }
            };
            
            class Queue2 : public Queue
            {
                int size;
                int* queue;
            public:
                Queue2() {
                    size = 0;
                    queue = new int[100];
                }
                void print() {
                    if (size == 0)
                    {
                        cout << "Queue is empty" << endl;
                        return;
                    }
                    for (int i = 0; i < size; i++) {
                        cout << queue[i] << endl;
                    }
                    return;
                }
            };
            
            int main() {
                Queue q1;
                q1.add(42); q1.add(2); q1.add(8);  q1.add(1);
                q1.print();
            
                Queue2 q2;
                q2.add(3); q2.add(66); q2.add(128);  q2.add(5); q2.add(111); q2.add(77890);
                q2.print();
            
                return 0;
            }'

我的输出:

42 <- 2 <- 8 <- 1 <-
Queue is empty

预期输出:

42 <- 2 <- 8 <- 1 <-
3
66
128
5
111
77890

【问题讨论】:

    标签: c++ c++11 visual-c++


    【解决方案1】:

    成员是继承的(无论是否私有)。不清楚为什么您在派生类中重复成员,然后期望基类方法正在使用它们。

    class Queue {
                int size;
                int* queue;
    

    Queues 方法正在使用这些成员。另一方面,

    class Queue2 : public Queue {
                int size;
                int* queue;
    

    派生类中的那些成员会影响基类中的成员。因此,在main 中,当您使用Queue2 时,您将通过调用Queue::add 来填充基础Queue::queue 中的queue 成员,然后尝试通过调用Queue::print 来打印Queue2::queue 的内容。

    您可以通过将基中的成员声明为 protected 并在派生中删除它们来修复它:

    class Queue {
        protected:
                int size;
                int* queue;
        ....
    
    
    
    class Queue2 : public Queue {
             // int size;    // remove them
             // int* queue;  // Queue2 already has those members inherited from its base
    

    Live Example

    最后但同样重要的是,您的代码中没有覆盖。要让Queue2::print 覆盖Queue::print,该方法需要在基类中为virtual。然后,您应该在派生类中将该方法声明为override,以在没有覆盖时出现编译器错误:

    struct Queue {
        virtual void print() {}
    };
                
    struct Queue2 : Queue {
        void print() override {}
    };
    

    为了说明派生对象有两个成员,一个来自基类,一个来自派生类,即使这两个成员具有相同的名称,请考虑以下示例:

    #include <iostream>
    
    struct base {
        int value = 42;
        void print(){
            std::cout << value << "\n";   // refers to base::value
                                          // inheritance will not change that
        }
    };
    
    struct derived : base {
        int value = 12;
        void set_value(int v){
            value = v;          // refers to derived::value
            base::value = 12;   // fully qualifying the name allows to 
                                // refer to base::value
        }
    };
                
    int main() {
        derived d;                
        d.set_value(10000);
        std::cout << d.value << "\n"; // refers to derived::value
        std::cout << d.base::value << "\n"; // refers to base::value
    }
    

    输出是:

    10000
    12
    

    当您将成员设为私有时,它们仍然存在于派生类中,但name lookup 将在找到derived::value 时停止,因此在派生类中value 指的是derived::value

    【讨论】:

    • 谢谢,在 Class Queue 中转换为 protected 并删除 Class Queue2 中的变量后,它工作正常。
    • 有一个疑问,我不明白为什么在基类和派生类中使用私有会导致问题。你能解释一下为什么使用私有类队列{私有:int大小会出现问题吗? int* 队列;公共:无效打印(){} ....}类队列2:公共队列{私有:int大小; int* 队列;公共:无效打印(){}}
    • @CharanSiddartha 我想我已经解释过了。见编辑
    猜你喜欢
    • 1970-01-01
    • 2021-11-26
    • 2013-02-05
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多