C + + Fog Landscape 4: The confusion of polymorphism, the copying of objects?

Source: Internet
Author: User

As an object-oriented language, C + + has three main features of object-oriented: encapsulation, inheritance, polymorphism. In the process of learning polymorphism, it is found that C + + differs greatly from other languages (pit? )。 The use of the = operator in C + + and the memory model presented by C + + do not seem to be the pattern I am accustomed to, between copying and referencing two different actions, or it is easy to write the code that has the problem, so it leads to this article today, we come to talk about = The story behind the operator.

1. Some strange polymorphic

Come on, first on the code, we'll look at the code of the polymorphic nature from two paragraphs, strange where.

class bird {public:    voidfly() {        "I can fly." << endl;    }};class penguin:public bird {public:    voidfly() {        "I can‘t fly." << endl;    

Above is a class definition of two inheritance relationships. The Penguin(Penguin) class inherits the bird class. In the bird class, The Fly () function is a virtual function that can be overwritten by Penguin . Let's see how the correct polymorphic code should be written:

intmain() {    bird* b1;    penguin p;         b1 = &p;    b1->fly();   //打印出:"I can‘t fly." }

The compiler uses the contents of the pointer, not its type, to determine which function should be called. Therefore, the corresponding fly () function is called because the address of the Penguin object is stored in the bird pointer.
So each bird subclass can be implemented independently of a function fly (). This is how polymorphism is used. There can be several different subclasses with a function with the same name but with different implementations.

Aha, these things look perfect. But programmers familiar with Java and Python should write the following code like I do:

intmain() {    penguin p;    bird b = p;     b.fly//打印出:"I can fly."    }

fxxk, is this still not my familiar polymorphism? Why the output is different from what I imagined. No, I have to try another method.

intmain() {    penguin p;    ((bird)p).fly//同样是打印出:"I can fly."    }
2. What's the problem?

Well, the above two pieces of code I would like to make a lot of Java or Python programmers deeply puzzled, it seems that C + + and our familiar language want to go far. In fact, this goes back to the topic that we are going to talk about today, and then we hit analyze the last two pieces of code:

intmain() {    penguin p;    bird b = p;     b.fly//打印出:"I can fly."    }

In fact, the core point of this code is to figure out what the = operator really means in bird B = P statement.

In order to explain this = operator, let's continue to look at the following code.

intmain() {    penguin p;    bird &b = p;     b.fly//打印出:"I can’t fly."    }

There is the magic of wood, let us confused problem solved, but added a & operator.
In C + +, the= operator represents a copy of the

    • Bird B = P
      Represents B as a bird object that regenerates a new bird object with P copy. So this is a copy operation, and the copy is an object.
    • Bird &b = P
      Represents B as a reference to a bird object that regenerates a reference to a new bird object by copying the address of P. So this is also a copy operation, which is a copy of an object reference. So by this reference, the dynamic call to the P object is the real function.

Well, after explaining the last piece of code, let's go ahead and look at the second code.

intmain() {    penguin p;    ((bird)p).fly//同样是打印出:"I can fly."    }

Why is it that we cannot output the results we want after we force the type conversion? That's because

In addition to pointers and reference types, the C + + compiler determines the address of the calling function statically through the type at compile time.
In this sentence, we are not difficult to understand the results of the previous code output, so we want to better use polymorphic, we must use good pointers and references.

3. Analysis of puzzles in other languages
    • Java
      The manipulation of pointers and object copies is completely discarded, so the = in Java is all references to copied objects. That's the shallow copy we're talking about. (The object copy is a deep copy, because a new object is generated, and the original object does not use the same memory space ).

    • Python
      In common with Java, it is an object reference. The only difference is that Python is a dynamic language, and when it comes to polymorphism, it relies more on the duck type than on the native inheritance.

    • Golang
      Same as Python, depending on duck type.

C + + Fog Landscape 4: The confusion of polymorphism, the copying of objects?

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.