C ++ Inheritance and base_class virtual keyword with derived class

Sorry, I better rewrite my question.

I have a base class ABCname that has a function name.

void saysez(ostream &os) const // this is NOT virtual!!
            { os << sez; }

and the name of the derived class DEF, which also have a function name

void saysez(ostream &os) const { os << extra << " ";
                         scary::saysez(os);

So, as you can see from the above code, both have the same signature.

In my understanding, if the keyword is virtualnot specified, it should use the function of the base class, but in my walkthrough, infer that it uses a derived function.

So, I wonder why it uses a derived function over a base function?

Below is a call from int main

     w.saysez(cout);  cout << '\n';

w is an object from a derived class.

Below is a link to the cut code with exit

http://codepad.org/Pz5jwMVP

+3
4

-, , , .

, - . ,

scary, , scary . - , , , , - ? . .

, w w.saysez(cout); cout << '\n';, scary , -, w , , . - saysez .

, .

+1

. . witch - void saysez (ostream &), w.saysez, saysez, .

. " ", w :

((scary*)&w)->saysez(cout)

"Double Double".

, , virtual .

+2

, :

#include <iostream>
using namespace std;
#include <cstring>

class scary {
    char is[31];
    char sez[31];
public:
    scary() {
        strcpy(is, "Creep");
        strcpy(sez, "boooo");
    }
    scary(const char i[], const char s[])
    {
        strcpy(is, i); strcpy(sez, s);
    }
    virtual void sayis(ostream &os) const { os << is; }
    void saysez(ostream &os) const // this is NOT virtual!!
        { os << sez; }
};

ostream &operator<<(ostream &os, const scary &x) {
    x.saysez(os);
    os << ", said the ";
    x.sayis(os);
    return os;
}

class ghost: public scary {
public:
    ghost():scary("Ghost", "Boo!")
    {
    }
};

class witch: public scary {
    char extra[31];
public:
    witch(): scary("Witch", "Toil and Trouble") {
        strcpy(extra, "Double, Double");
    }
    void saysez(ostream &os) const {
        os << extra << " ";
        scary::saysez(os);
    }
};

int main() {
    scary s; 
    ghost g; 
    witch w;
    cout << s << '\n' << g << '\n' << w << '\n';
    return 0;
}

:

boooo, said the Creep
Boo!, said the Ghost
Toil and Trouble, said the Witch

witch sez "Toil and Trouble", saysez, witch, extra scary saysez , sez. , ( ) .

+1

, w witch, , , witch:: saysez(). , , :

scary* w2 = new witch();
w2->saysez(cout);

... scary:: saysez().

, , . , . Foo, Foo:: MyNonVirtualFn(), Foo:: MyNonVirtualFn(). Foo , MyNonVirtualFn(), , , Foo, MyNonVirtualFn() - . Foo:: MyNonVirtualFn(), . ( , MyNonVirtualFn() , , , - , .)

Virtual functions are different in that if you call Foo :: MyVirtualFn () on an object, it checks if the object is an instance of the subclass of Foo that overrides MyVirtualFn (). For example:

Base* base = new Base();
Derived* derived = new Derived();
Base* derived_as_base = derived;

base->MyNonVirtualFn();  // Calls Base::MyNonVirtualFn().
derived->MyNonVirtualFn();  // Calls Derived::MyNonVirtualFn().
derived_as_base->MyNonVirtualFn();  // Calls Base::MyNonVirtualFn() because it called on a Base*.

base->MyVirtualFn();  // Calls Base::MyNonVirtualFn().
derived->MyVirtualFn();  // Calls Derived::MyNonVirtualFn().
derived_as_base->MyVirtualFn();  // Calls Derived::MyNonVirtualFn() because it uses a lookup table.
+1
source

All Articles