Effective C + + object-oriented and inheritance

Source: Internet
Author: User

turn from: http://www.kuqin.com/language/20120802/323769.html

1: Subclasses do not overwrite the non-virtual functions of the parent class.

2: Subclasses do not overwrite default parameters inherited from the parent class

3: Assignment problems between subclasses and parent classes

1: Subclasses do not overwrite the non-virtual functions of the parent class.

For the sake of explanation, first look at a simple example.

Class A
{public
    : 
        A (int d):d ata (d) {  }

        void print ()
        {
            cout<< "A print ..." << data<<endl;
        }

        virtual void Test (int i=2)
        {
            cout<< "A test ..." <<i<<endl;
        }
    Private: 
        int data;

Class B:public a
{public
    : 
         
        B (int D): A (d) {  }
        void print ()
        {
            cout<< "b print" ... "<<endl;
        } 
        virtual void Test (int i=4)
        {
            cout<< "B test ..." <<i<<endl;
        }
};
  
Test Code
int main () { 
    {
        b b (5); 
        B.print ();
        A *a=&b; 
        A->print ();  
        cout<<endl;
        B.test ();
        A->test ();
        cout<<endl;
        A a1=b;
        A1.test ();
    }
     
     GetChar ();
     return 0;
} 
  

Run Results screenshot:

In the example, pointer A is pointing to object B, but the print method they are calling is not the same. This involves the problem of static binding and dynamic binding. The static type of a is the dynamic type of the a,a, but the static type of the b,b and the dynamic type are both B, because the static type is the type of the declaration, and the dynamic type is the type that it really points to. Another point is that non-virtual methods are statically bound, and virtual methods are dynamically bound. Print is a non-virtual method, which is a static binding, calling the method of its own object declaration type, so a call to Print,b of a is called B's Print method. I think we'd like to know how C + + is implementing dynamic binding. We all know that classes with virtual methods have a virtual method table, each instance of each object has a pointer to the virtual method table, the subclass inherits the virtual method of the parent class, or the dummy method of the parent class, if the subclass repeats the virtual method of the parent class, Then the corresponding pointer in the virtual table points to the method of the subclass to overwrite the parent class, or the method that points to the parent class if the subclass does not overwrite the virtual method of the parent class, thus forming a dynamic binding. Different subclasses write the virtual methods of the parent class in their own way, showing different behaviors, which is polymorphism. In multiple inheritance, each object may have more than one virtual table. Then its instance will have multiple pointers to the virtual table, and if multiple parent classes have the same method, then you cannot invoke this method directly with this instance, because the compiler has no idea which method it should call, and you want to specify the method of that parent class. When you indicate which parent class, the compiler can invoke the corresponding method in the corresponding virtual table through the corresponding pointer. So what is the process of an instance calling a virtual method, have you ever thought about it? In fact, the above mentioned a little, roughly three steps:

1: According to the object's vptr pointer to find its virtual method table VTBL;

2: Find the corresponding pointer of the called method in VTBL;

3: Call the method that the pointer points to in 2.

2: Subclasses do not overwrite default parameters inherited from the parent class

This is actually related to the static binding and dynamic binding problem, I think the above has made it clear that the default is also a static binding, there is no doubt, because it was established at the compile time, and virtual methods do dynamically bind, You have nothing to do with the static bound thing and the dynamically bound thing, but you've got an inch. To overwrite static things in subclasses will cause problems, sorry, the parent class is static, regardless of the subclass, so when the subclass does not overwrite the default parameters inherited from the parent class, Subclasses may appear to be schizophrenic behavior, the above one is proof.

It's all about virtual methods, so what about non-virtual methods, and how object instances call Non-virtual methods? How does the Non-virtual method come true? Non-virtual methods are implemented as normal C functions, so their calls do not need to find a pointer like a virtual method, and then invoke the corresponding method through the pointer.

3: Assignment problems between subclasses and parent classes

The first thing to convert a parent class to a subclass is best not to do so, because many of the attributes of subclasses do not have a parent class, and when you use a subclass that converts from a parent class to a subclass, it is likely to go wrong. Next we'll focus on converting subclasses into parent classes. Or the above example to illustrate the problem.

b b (2);

A a=b;//Call copy constructor

a=b;//Call operator=

The above two lines of code, the first one to instantiate an object B, the second line B to give a, then how to assign B to a, where the actual call is not operator=, but copy constructor, because the construction of an object must call constructor, or copy Constructor, then here must be called copy constructor,operator= is just an assignment action, an object has not yet been constructed how to assign value to him, in operator= is not used to help you construct the object of Oh, In the third line, a has been constructed, so it is true that the value of the call is operator=. In a word, an object as a left value, the first affirmation of the call is copy constructor, was initialized (allocated memory), after the operation is assigned value. An object as a by-value parameter, then each call is copy constructor, rather than operator=, we will generally say that the argument to the formal parameters, in fact, the argument is constructed with a formal parameter.

Assigning B to a, or assigning a part of B to A,a, is a complete a, which knows nothing about B and does not show any behavior of B, so by value is violent and performance-intensive and does not show polymorphic behavior. So avoid using by value and use by reference as much as possible.

This is the end.

Effective C + + series:

Effective C + + constructor destructor assignment operator

Design and declaration of effective C + + classes and functions

Effective C + + object-oriented and inheritance

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.