简体   繁体   English

C ++中的抽象类,函数继承和覆盖

[英]Abstract Classes, Function Inheritance and Override in C++

Class A is abstract class. A类是抽象类。 I would like to call the same method from different subclasses. 我想从不同的子类调用相同的方法。 There are no errors from compiler, but program crashes. 编译器没有错误,但程序崩溃。

class A
{
    protected:

        MA* array1[10]; //static array of pointers to some other class
        int st;

    public:
    ...
        virtual string ToString() const
        {
            stringstream ss;

            for(int i=0;i<st;i++)
            {
                ss<<array1[i]->getname()<<",";
            }

            return ss.str();
        }
};

ToString method works in both classes A and B but doesn't work in class C ToString方法在A类和B类中都有效,但在C类中不起作用

class B: public A
{
    private:
    ...

    public:

        string ToString() const
        {
            return A::ToString();
        }
};

class C: public A
{
    private:
    ...

    public:

        string ToString() const
        {
            return A::ToString();    
        }
};

This is main: 这是主要的:

A* P[5];
P[0]=new B();
P[1]=new C();
P[0]->ToString();
P[1]->ToString();

I'm pretty sure it works in neither A nor B. 我很确定它在A或B中都不起作用。

The only reason it seems to would be Undefined Behaviour. 似乎唯一的原因就是不确定的行为。

Here is the issue: 这是问题:

First, your derived classes (B and C) are inheriting Class A's public member functions (I know this is obvious looking at the code - but, we have to start somewhere); 首先,您的派生类(B和C)继承了A类的公共成员函数(我知道在代码中这是显而易见的-但是,我们必须从某个地方开始); however , the derived classes are overriding your Parent class's (Class A) member functions by utilizing the same function identifier. 但是 ,派生类通过利用相同的函数标识符来覆盖父类的(类A)成员函数。 The issue here is that the Parent does not expect the inheriting - derived - classes to override the parent classes, which they are doing. 这里的问题是,父级不希望继承的(派生的)类覆盖它们正在执行的父级。 The Parent only expects the derived classes to inherit; 父类只希望派生的类继承; therefore, when you call the function of Class B and Class C, something weird is happening. 因此,当您调用B类和C类函数时,发生了一些奇怪的事情。

Two issues arise as a result: first (1), that which we discuss above (see code, below); 结果产生了两个问题:首先是(1),我们在上面已经讨论过(参见下面的代码); second (2), the derived classes do not have access to the Parent class's member function definition unless called via an object of the parent class ( which would not normally be the case, here, were your functions properly declared ). 第二(2),派生类无法访问父类的成员函数定义,除非通过父类的对象调用( 通常情况并非如此,此处正确声明了您的函数 )。

So, what can you do? 所以,你可以做什么? Use the override keyword in your Parent declaration and in your child declaration. 在父声明和子声明中使用override关键字。

For instance: 例如:

Class A
{
    private:
    ....
    public:
    virtual returntype functionIndentifier(parameters) override;
};

Now, that has to do with inheritance; 现在,这与继承有关。 however, in C++, were your class actually abstract, you would need to use a pure virtual function (by setting the function equal to 0), not defining the function in the abstract class, leaving out the override keyword, and ensuring each child class implements - and defines - the function. 但是,在C ++中,如果您的类实际上是抽象的,则需要使用一个纯虚函数(通过将函数设置为0),而不是在抽象类中定义该函数,而忽略override关键字,并确保每个子类实现-并定义-功能。

Class A
{
    private:
    ....
    public:
    virtual returntype functionIndentifier(parameters) = 0;
};

pure virtual functions force a child class to both inherit and implement - define - a member function to the child class's needs. 纯虚函数强制子类继承实现-定义-满足子类需求的成员函数。 Here, if the pure virtual class were not defined in your child's class definition, then - I believe - you would in fact encounter a compile-time error. 在这里,如果您的孩子的类定义中没有定义纯虚拟类,那么-我相信-您实际上会遇到编译时错误。

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

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