繁体   English   中英

与C ++继承混淆

[英]Confused with C++ Inheritance

我对以下代码的输出很困惑:

#include <iostream>

using namespace std;

class Parent
{
public:
    Parent() : x(2) { }
    virtual ~Parent() { }

    void NonVirtual() { cout << "Parent::NonVirtual() x = " << x << endl; }

private:
    int x;
};

class Child : public Parent
{
public:
    Child() : x(1) { }
    virtual ~Child() { }

    void NonVirtual() { cout << "Child::NonVirtual() x = " << x << endl; }

private:
    int x;
};

int main()
{
    Child c;
    Parent* p = &c;

    c.NonVirtual();  // Output: Child::NonVirtual() x = 1
    p->NonVirtual(); // Output: Parent::NonVirtual() x = 2
    // Question: are there two x'es in the Child object c?
    //           where is x = 2 from (we have not defined any Parent object)?

    cout << sizeof(c) << endl;
    cout << sizeof(*p) << endl;

    return 0;
}

上面的代码说明了这样一个事实:在C ++中,只有标记为virtual函数才会被覆盖 你在这里拥有的是黯然失色 ,而不是压倒一切。 which is the normal inheritance behavior you expect, but if you don't declare it virtual, then the behavior is based purely on (ie declared type). 在重写和继承中,行为基于 ,这是您期望的正常继承行为,但如果您不将其声明为虚拟,那么该行为完全基于 (即声明的类型)。 由于p被声明为Parent *类型,它使用Parent中的实现,而c被声明为Child类型,因此它使用Child类型给出的版本。 如果您将方法声明为virtual,那么在这两种情况下,它都会在运行时查找该函数的相应版本并调用Child类中给出的版本。

我还应该补充一点,你有两个不同的x变量...如果你想在基类和派生类之间共享变量,你应该在基类中将它标记为“受保护”(尽管我认为它通常很差)设计这样做)。 您在Child中声明的x变量是与Parent中的变量不同的变量。 请记住,x在Parent中是私有的,因此在Child中创建名为x的第二个变量之前,名称x在Child中没有任何意义。

这里没有什么可以混淆的。 当您调用非虚函数时,无论分配给哪种类型,都会调用该类型的方法。

这里,Parent * p和子地址相同,但是赋值给Parent。 因此,所有非虚拟方法都会调用父方法,而不是子方法。

请记住,当您使用继承(尤其是公共的继承)时,子项会自动派生所有父项的方法和成员。 因此,即使您指定了孩子的指针,但是分配也发生在父母的类型上。

你唯一需要确定的是你不要在这里删除P作为P和子C共享相同的地址。

暂无
暂无

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

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