简体   繁体   English

从派生类访问基类中的受保护成员

[英]Accessing protected member in base class from a derived class

I have the following snippet of code: 我有以下代码片段:

const int DATE_LENGTH = 6;

class BaseClass {
    protected:
        int date[DATE_LENGTH];
        int year;
    public:
        BaseClass(){}
        BaseClass(int *d) {
            for (int i = 0; i < DATE_LENGTH; i++) { date[i] = d[i];}
            year = 1900 + date[4] * 10 + date[5];
        }
        void printYear() {
            cout << year << endl;
        }
};

class DerivedClass : public BaseClass {
    public:
        DerivedClass() {}
        void printYear() {
            cout << year << endl;
        }
};

int main(int argc, char *argv[]) {
    int dob[] = {1, 6, 1, 0, 9, 0};
    BaseClass base(dob);
    base.printYear(); // prints 1990

    DerivedClass derived;
    derived.printYear(); // prints 1439156608
}

I'm having trouble understanding why the output from printYear() in my derived class is outputting junk. 我无法理解为什么派生类中printYear()输出会输出垃圾。 Am I missing something very obvious? 我是否缺少一些显而易见的东西?

Any help would be appreciated! 任何帮助,将不胜感激!

Your program has undefined behaviour. 您的程序具有未定义的行为。 The default constructor of DerivedClass , which you are using, does not initialize the year member. 您使用的默认DerivedClass构造函数不会初始化year成员。

If you wanted to initialize the base member, you could do that by calling an appropriate base constructor, or by assigning the value directly. 如果要初始化基本成员,可以通过调用适当的基本构造函数或直接分配值来实现。

DerivedClass() { year = 1999; }

The default constructor of class BaseClass BaseClass的默认构造函数

BaseClass(){}

does not initislize data members date and year 不会滥用数据成员的dateyear

This default constructor is called by the default constructor of class DerivedClass when you create object derived 创建派生对象时,类DerivedClass的默认构造函数调用此默认构造函数。

DerivedClass derived;

So these data members have arbitrary values and your program has undefined behaviour. 因此,这些数据成员具有任意值,并且您的程序具有未定义的行为。

Change the derived class the following way 通过以下方式更改派生类

class DerivedClass : public BaseClass {
    public:
        using BaseClass::BaseClass;
        DerivedClass() {}
        void printYear() {
            cout << year << endl;
        }
};

and create object derived as 并创建派生为

DerivedClass derived( dob );

Or instead of the using declaration you can yourself explicitly define a constructor in class DerivedClass that has one parameter of type int * and calls the corresponding constructor of the base class. 或者,也可以代替using声明,自己在类DerivedClass中显式定义一个构造函数,该构造函数具有一个int *类型的参数并调用基类的相应构造函数。 For example 例如

class DerivedClass : public BaseClass {
    public:
        DerivedClass() {}
        DerivedClass( int *d ) : BaseClass( d ) {}
        void printYear() {
            cout << year << endl;
        }
};

The other provided answers only deal with part of the issue, partly because the date itself is never defined in the DerivedClass default constructor of either currently provided, and the other part being the BaseClass default constructor still has no defined value for either class variable (date or year). 提供的其他答案仅处理部分问题,部分原因是日期本身从未在当前提供的其中一个的DerivedClass默认构造函数中定义,而另一部分是BaseClass默认构造函数仍未为这两个类变量定义任何值(日期或年份)。 If the following code is used, depending what the default date and year should be, no additional change is actually required for the DerivedClass. 如果使用以下代码,则取决于默认日期和年份,实际不需要对DerivedClass进行其他更改。

#include <iostream>

using namespace std;

const int DATE_LENGTH = 6;

class BaseClass {
    protected:
        int date[DATE_LENGTH];
        int year;
    public:
        BaseClass()
        {
            int date[] = {1, 6, 1, 0, 9, 0};
            year = 1900 + date[4] * 10 + date[5];
        }
        BaseClass(int *d)
        {
            for (int i = 0; i < DATE_LENGTH; i++) { date[i] = d[i];}
            year = 1900 + date[4] * 10 + date[5];
        }
        void printYear() {
            cout << year << endl;
        }
};

class DerivedClass : public BaseClass {
    public:
        DerivedClass() {}
        void printYear() {
            cout << year << endl;
        }
};

int main(int argc, char *argv[])
{
    int dob[] = {1, 6, 1, 0, 9, 0};
    BaseClass base(dob);
    base.printYear(); // prints 1990

    DerivedClass derived;
    derived.printYear(); // prints 1439156608
    return 0;
}

Application Output 应用输出

1990
1990

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

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