Objective C ++ object-oriented and inheritance

Source: Internet
Author: User

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

2: do not override the default parameters inherited from the parent class.

3: Assignment between subclass and parent class

 

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

For ease of interpretation, let's take a look at a simple example.

 Class  A {  Public  : (  Int  D): Data (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 ;}};  //  TestCode  Int  Main () {B (  5  ); B. Print (); * A = &B; -> Print (); cout < Endl; B. Test (); -> Test (); cout < Endl; A A1 = B; a1.test ();} getchar ();  Return   0  ;} 

Running result:

In this example, pointer A points to object B, but the print method they call is not the same. Static binding and dynamic binding are involved. The static type of A is a, the dynamic type of A is B, and the static and dynamic types of B are both B,Because the static type is the type at the time of declaration, the dynamic type is the type to which it actually points. Another point is that the non-virtual method is static binding, and the virtual method is dynamic binding.Print is a non-virtual method. It is static binding and calls its own object declarative method. Therefore, a calls print of A and B calls print of B. I think we 'd like to know how C ++ implements dynamic binding. We all know that classes containing virtual methods have a virtual method table. Each object instance has a pointer pointing to this virtual method table, and the subclass inherits the virtual method of the parent class, you can also override the virtual method of the parent class. If the subclass overrides the virtual method of the parent class, the corresponding pointer in the virtual table directs to the subclass to override the parent class method, if the subclass does not override the virtual method of the parent class, it still points to the method of the parent class, forming a dynamic binding. Different sub-classes override the Virtual Methods of the parent class in their own way, and show different behaviors. This is polymorphism. In multi-inheritance, each object may have multiple virtual tables, so its instance will have multiple pointers to the virtual table. If multiple parent classes have the same method, then you cannot directly use this instance to call this method, because the compiler does not know which method it should call. You must specify the method of the parent class, if you specify the parent class, you can call the corresponding method in the virtual table through the corresponding pointer. So how does an instance call a virtual method? Have you ever thought about it? As mentioned above, there are three steps:

1: Find the virtual method table vtbl Based on the vptr pointer of the object;

2: Find the pointer of the called method in vtbl;

3: Call the method pointed to by the pointer in 2.

 

2: do not override the default parameters inherited from the parent class.

In fact, this article still involves static binding and dynamic binding. I have already made it clear that the default value is static binding, which is undoubtedly true, because it has been confirmed during the compilation phase, and the virtual method is indeed dynamically bound, there is no problem in mixing static and dynamic bindings, however, if you still have to overwrite static content in the subclass, it will lead to problems. Sorry, the parent class does not care about static content in the subclass, but only static content, therefore, when the child class does not override the default parameters inherited from the parent class, the Child class may be split. The column above is proof.

 

All of the above are about virtual methods. How do we call non-virtual methods when using non-Virtual Methods in an object instance? How is a non-virtual method implemented? Non-virtual methods are implemented as common C functions, so they do not need to find a pointer like virtual methods, and then call the corresponding method through this pointer.

 

3: Assignment between subclass and parent class

First, it is best not to convert the parent class to a subclass because many features of the subclass do not exist at all. When you use a subclass converted from the parent class as a subclass, problems may occur. Next we will focus on converting child classes into parent classes. The above example is used to illustrate the problem.

B (2 );

A A = B; // call copy constructor

A = B; // call operator =

In the above two lines of code, the first line first instantiates an object B and the second row B assigns it to A. How does B assign it to, in fact, we call not operator =, but copy constructor. Because we must call constructor or copy constructor to construct an object, we must call copy constructor. Operator = is only a value assignment, how can I assign values to an object before it is constructed? Operator = is not used to help you construct the object. In the third row, A has been constructed, in this case, operator = is called. In a word, when an object is taken as the left value, the copy constructor must be called for the first time. After initialization (memory allocation), the subsequent operations are assigned values. If an object is used as a parameter in the form of by value, copy constructor is called every time, instead of operator =. We generally say that the real parameter is assigned to the form parameter. In fact, a form parameter is constructed using the real parameter.

Assigning B to a is to assign a part of B to a, and a is a complete a. It knows nothing about B and does not show any behavior of B, so by value is very violent and performance-consuming, and there will be no polymorphism. Therefore, avoid using by value and try to use by reference.

Stop it. It will not be resumed...

 

Valid C ++ series:

Objective C ++ constructor destructor assignment operator

Design and declaration of Objective C ++ classes and functions

Objective C ++ object-oriented and inheritance

Author: Chen taihan

Blog: http://www.cnblogs.com/hlxs/

 

 

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.