S
S
sddvxd2018-04-22 14:14:56
OOP
sddvxd, 2018-04-22 14:14:56

Why make a virtual function in derived classes?

Good afternoon!
Sometimes I come across examples like this:

class BaseABC{
public:
    virtual void test()const = 0;
...
}

class Heir : public BaseABC{
public:
    virual void test()const;
...
}

Why make an overridden function virtual in a derived class? This is done so that the Heir successor can override test? If I want to call like this:
//head.h
class TooHeir : public Heir{
public:
    void test()const;
...
}

//realize.cpp

void TooHeir::test()const{
std::cout<<"Invoked test() of object \"TooHeir\"";
}

//main.cpp

TooHeir * pth = new TooHeir;
pth->test();

In the end, here's the question: virtual functions only affect 1 level of inheritance? As far as I know, destructors act on an infinite number of inheritance chains BaceABC->Heir->TooHeir->...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sddvxd, 2018-04-22
@sddvxd

Figured it out thanks to the article https://msdn.microsoft.com/ru-ru/library/0y01k918.aspx

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question