Diamond Inheritance (virtual function)-> Diamond virtual Inheritance (virtual function)-> Polymorphism Series problem __ function

Source: Internet
Author: User
Tags function prototype

Reader's attention: When reading this article, you must have an understanding of the object model in the inheritance, because some examples in this article do not give the class member variable, if you do not understand the inherited object model, it may be slow to think, you feel unsure of the following "inheritance."

inherited polymorphic

Polymorphism literally means "multiple states." In object-oriented languages, many different implementations of interfaces are polymorphic. The straightforward point of understanding is that when different objects receive the same message, they produce different actions. (with the Wind down)

Polymorphism can be divided into static polymorphic and dynamic polymorphic.
Static polymorphism: is done during the compiler, also known as early binding;
Dynamic polymorphism: The actual type of the referenced object is judged during the execution of the program (not at compile time), and the corresponding method is called according to its actual type, also called dynamic binding.

the constitutive condition of dynamic polymorphism : To have a virtual function, and must be rewritten, one in the base class, one in the derived class. (The function prototype of the virtual function, the return value, the function name, the argument list must be the same), the virtual function must be called through a reference to a base class type or a pointer.

For a simple example:

Class A
{public
:
    A (int x)//constructor
    {
        a = x;
    }
    virtual void print ()//virtual function
    {
        cout << "A::" << a << Endl;
    }
Private:
    int A;
};

Class B:p ublic a
{public
:
    B (int x, int y)
        : A (x)
    {
        B = y;
    }
    virtual void print ()//rewrite
    {
        cout << "B::" << b << endl;
    }
Private:
    int b;
};

void Test ()
{
    a A ();
    b b (15,20);
    A *c = &a;
    C->print ();
    c = &b;
    C->print ();

}

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

With one exception: covariant; return value type is different (a reference or pointer to a base class returned in a base class that returns a reference or pointer to a derived class) overload & the difference between a hidden & rewrite of the same name

Overloading: Overloading can only be a member function of a class, must be within the same scope (that is, in the same class), the member function must have the same name, and the argument list (number, order, type) can be different.

Hidden with the same name: As long as the member (can make a member variable can also be a member function) the same name (one in the base class, one in the derived class), and the argument list, return value does not have a relationship, and to use the object of the derived class to invoke the same name.

Overrides: There is a virtual function, and one is in the base class, one in the derived class, and the function's prototype is the same (except covariant) which member functions can be used as virtual functions. Which cannot be used as a virtual function.

1. Constructor (copy construction)
No, because the constructor can construct the object, the object can have the virtual table, but the virtual function first calls the virtual table, in the virtual table to find the constructor, the two contradictions.

2. Inline function
No, the reason is very simple, that inline function is to expand directly in the code to reduce the cost of function calls, virtual function is to be able to carry out the object after the inheritance of their own actions, it is impossible to unify. (again, the inline function is expanded at compile time, and the virtual function can be dynamically defined at runtime)

3. Static member function
No, because static member functions are global, and for all classes, it's a common code that doesn't belong to a specific object, so he has no need for dynamic bonding.

4. Friend function
No, because C + + does not support the inheritance of friend functions, the argument that there is no virtual function for functions that do not have inherited attributes.

5. Assignment operator overloading
OK, but not, although it is possible to define operator= as a virtual function, it is best not to do so, and it is easy to confuse when used.

6. destructor
OK, and it's a good idea to declare a destructor of a base class as a virtual function. Because the destructor is special because the destructor of the derived class is not the same as the destructor name of the base class, but constitutes an overlay, the compiler does a special deal to reduce the space overhead. inherited

In the previous blog we mentioned the inherited object model, and this time we still talked about the inherited object model, but this time we added a virtual function to the class. When it comes to virtual functions, let's dissect them and give a simple example:

Class A
{public
:
    virtual void Funtest1 ()
    {
        cout << "A::funtest1" << Endl;
    }
    virtual void Funtest2 ()
    {
        cout << "A::funtest2" << Endl;
    }
    virtual void Funtest3 ()
    {
        cout << "A::funtest3" << Endl;
    }
    virtual void Funtest4 ()
    {
        cout << "A::funtest4" << Endl;
    }
;

typedef void (*PVTF) ();

void Printvpt ()
{
    a A;
    pvtf* PVTF = (pvtf*) (* (int*) &a);

    while (*PVTF)
    {
        (*PVTF) ();
        pvtf++
    }
}
void Test ()
{
    cout << sizeof (A) << Endl;

    PRINTVPT ();
}

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

That is, a virtual function is defined in a class, then after the object is created, the compiler creates a virtual table with the first four bytes of the object holding the address of the virtual table, and the virtual tables hold the addresses of each virtual function. Single Inheritance

In the first case: The derived class does not have the virtual function overridden or its own virtual function, the virtual function of the base class is directly inherited, and the derived class regenerates a virtual table, and the virtual table in the base class is not inherited. (The virtual table address in the derived class is not the same as the virtual table address of the base class, and the virtual list address is different, but the virtual function is the same)

Class A
{public
:
    virtual void Funtest1 ()
    {
        cout << "A::funtest1" << Endl;
    }
    virtual void Funtest2 ()
    {
        cout << "A::funtest2" << Endl;
    }
    virtual void Funtest3 ()
    {
        cout << "A::funtest3" << Endl;
    }
    virtual void Funtest4 ()
    {
        cout << "A::funtest4" << Endl;
    }
;

Class B:p ublic A
{

};
typedef void (*PVTF) ();//function pointer

void PRINTVPT ()//Print virtual table
{
    b b;
    pvtf* PVTF = (pvtf*) (* (int*) &b);

    while (*PVTF)
    {
        (*PVTF) ();
        pvtf++
    }
}
void Test ()
{
    cout << sizeof (A) << Endl;
    cout << sizeof (B) << Endl;

    PRINTVPT ();
}

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

In the second case, a virtual function in the base class is overridden in a derived class and has its own virtual function.

If a virtual function in a base class is overridden in a derived class, the virtual function that is overridden by the derived class replaces the virtual function in the base class.

If the derived class defines a new virtual function, it is placed after the base class virtual function in the declared order.

Class A
{public
:
    virtual void Funtest1 ()
    {
        cout << "A::funtest1" << Endl;
    }
    virtual void Funtest2 ()
    {
        cout << "A::funtest2" << Endl;
    }
    virtual void Funtest3 ()
    {
        cout << "A::funtest3" << Endl;
    }
    virtual void Funtest4 ()
    {
        cout << "A::funtest4" << Endl;
    }
;

Class B:p ublic A
{public
:
    virtual void Funtest2 ()
    {
        cout << "B::funtest2" << Endl;
    }
    virtual void Funtest5 ()
    {
        cout << "B::funtest5" << Endl;
    }
;
typedef void (*PVTF) ();//function pointer

void PRINTVPT ()//Print virtual table
{
    b b;
    pvtf* PVTF = (pvtf*) (* (int*) &b);

    while (*PVTF)
    {
        (*PVTF) ();
        pvtf++
    }
}
void Test ()
{
    cout << sizeof (A) << Endl;
    cout << sizeof (B) << Endl;

    PRINTVPT ();
}

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function "> multiple Inheritance "

Here I mix all the possibilities into one: that is, the virtual functions of base class A and B are overridden in a derived class and also have their own virtual functions.

If the virtual functions in base class A and B are overridden in the derived class, replace the virtual functions in the base class A, B, respectively;

If a derived class has its own virtual function, it is placed after the virtual function in the first base class (that is, the Class A) virtual table in the declaration order.

Derived classes in multiple inheritance do not regenerate tables, but directly inherit the virtual tables of base class A and B directly.

Class A {public:virtual void Funtest1 () {cout << "a::funtest1" << Endl;

}
};
    Class B {public:virtual void Funtest2 () {cout << "b::funtest2" << Endl;

}
}; Class C:p ublic A, public B {public:virtual void Funtest1 () {cout << "c::funtest1" << Endl
    ;
    virtual void Funtest2 () {cout << "c::funtest2" << Endl;
    virtual void Funtest4 () {cout << "c::funtest4" << Endl;


}
};
    typedef void (*PVTF) ();//function pointer void printvpt ()//Print virtual table {c C;

    pvtf* PVTF = (pvtf*) (* (int*) &c);
        while (*PVTF) {(*PVTF) ();
    pvtf++;
    A A;
    B &b = c;
    PVTF = (pvtf*) * (int*) &b;
        while (*PVTF) {(*PVTF) ();
    pvtf++;
    } void Test () {cout << sizeof (A) << Endl;
    cout << sizeof (B) << Endl;
    cout << sizeof (C) << Endl;
PRINTVPT ();} 

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function "> diamond Inheritance" (Diamond inheritance)

The situation I have listed here is that the derived class has its own virtual function, which does not override the virtual function when inheriting, so as to make it more intuitive to see its object model. Because once overridden, in the object model you can not tell whether it is a derived class or a base class.

A diamond inheritance that contains virtual functions, the object model of a derived class is, in this case, a member variable is not given in the class, plus a member variable (good understanding of the point is understood as a function), the model is this:

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

B1 's virtual table contains the original base class A and the virtual function of derived class C, and the virtual table of B2 contains the original base class A virtual function. Similarly, if the virtual functions in the base class B1 and B2 are overridden in the derived class, replacing the virtual functions in the base class B1, B2 virtual tables, and replacing the virtual functions of Class A in B1 and B2.

class A {public:virtual void Funtest1 () {cout << "A::funtest1" <<
    Endl

}
};
    Class B1:public A {public:virtual void Funtest2 () {cout << "b1::funtest2" << Endl;

}
};
    Class B2:p ublic A {public:virtual void Funtest3 () {cout << "b2::funtest3" << Endl;

}

}; Class C:p ublic b1,public B2 {public:virtual void Funtest4 () {cout << "c::funtest4" << End
    L


}
};
    typedef void (*PVTF) ();//function pointer void printvpt ()//Print virtual table {c C;

    pvtf* PVTF = (pvtf*) (* (int*) &c);
        while (*PVTF) {(*PVTF) ();
    pvtf++;
    } B2 &b = C;
    PVTF = (pvtf*) * (int*) &b;
        while (*PVTF) {(*PVTF) ();
    pvtf++;
    } void Test () {cout << sizeof (B1) << Endl;
    cout << sizeof (B2) << Endl;
    cout << sizeof (C) << Endl;
PRINTVPT (); }

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function "> virtual Inheritance

Single inheritance (Virtual inheritance)

The object model should be this:

Class A
{public
:
    virtual void Funtest1 ()
    {
        cout << "A::funtest1" << Endl;
    }
};

class B:virtual Public A
{Public
:
    virtual void Funtest2 ()
    {
        cout << ' b::funtest2 ' << Endl;
    }
    virtual void Funtest1 ()
    {
        cout << "B::funtest1" << Endl;
    }
;
typedef void (*PVTF) ();//function pointer

void PRINTVPT ()//Print virtual table
{
    b b;
    pvtf* PVTF = (pvtf*) (* (int*) &b);

    while (*PVTF)
    {
        (*PVTF) ();
        pvtf++;
    }
    A &a = b;
    PVTF = (pvtf*) * ((int*) &a);
    while (*PVTF)
    {
        (*PVTF) ();
        pvtf++
    }
}
void Test ()
{
    cout << sizeof (A) << Endl;
    cout << sizeof (B) << Endl;

    PRINTVPT ();
}

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function "> diamond Virtual Inheritance

Class A {public:virtual void Funtest1 () {cout << "a::funtest1" << Endl;
int _a;
}; 
    Class B1:virtual public A {public:virtual void Funtest2 () {cout << "b1::funtest2" << Endl;
int _b1;
}; 
    Class B2:virtual public A {public:virtual void Funtest3 () {cout << "b2::funtest3" << Endl;
    virtual void Funtest1 () {cout << "b2::funtest1" << Endl;
int _b2;
}; Class C:p ublic B1, public B2 {public:virtual void Funtest3 () {cout << ' c::funtest3 ' << end
    L
    virtual void Funtest4 () {cout << "c::funtest4" << Endl;
int _c;

};

typedef void (*PVTF) ();
    void Printvft () {C c;//Create a derived class object d c._a = 1;
    C._B1 = 2;
    C._B2 = 3;
    C._c = 4;

    pvtf* pvtf= (pvtf*) * (int*) &c;
        while (*PVTF) {(*PVTF) ();
    pvtf++; } cout << Endl << Endl;
    PVTF = (pvtf*) * ((int*) &c + 3);
        while (*PVTF) {(*PVTF) ();
    pvtf++;

    } cout << Endl << Endl;
    PVTF = (pvtf*) * ((int*) &c + 7);
        while (*PVTF) {(*PVTF) ();
    pvtf++;
    } void Test () {cout << sizeof (C) << Endl << Endl;

PRINTVFT (); }

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

Object Model:

Diamond Virtual Inheritance (virtual function)-> Polymorphism series problem __ Function ">

Here's a summary: we can see from the Diamond virtual inheritance that contains virtual functions, it is in fact and does not contain virtual function of the diamond virtual inheritance object structure is not much, only the virtual function in the derived class to write to the first base class, in the original offset table address to inherit the virtual table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.