Deep Exploration C + + object Model (1)

Source: Internet
Author: User

Chapter One: About objects (object lessons)
Reading this chapter reminds me of a joke I saw a long time ago, writing a Hello World program, and as the level and position are different, the code changes. When I looked at it as a joke, it seems that the people who write this joke are not in the usual level. If you want to enable your code to adapt to the maximum of different operating environment, and the maximum reuse, then in the design and writing process need to consider a lot of problems, so the code has become not with the C language concise, efficient. And sacrificing these advantages in exchange for a better package. Of course, if you just want to print Hello World, you don't have to do that.

In C + + thinking way to solve the problem, for the C language has been very familiar with the people will be very unable to adapt. It takes a while to get used to it, otherwise it would be plausible to write the code. And can not managernent, must from the ideological thorough C + + (OO), if just according to gourd painting scoop, the result is likely to use C + + grammar to write C-style program. One of the typical low-level mistakes I've ever committed, it is an unconscious class that has unlimited expansion, takes no account of the multi-layered structure of the class (the base class-derived class), the need for attributes or methods to be added to the class, although it also uses some OO design methods, such as polymorphism, overloading, and so on, but in the end this class is huge In addition to the hard work in the current system, there may not be any reuse, and if another system needs a similar thing, it can only redesign a new class. And the most fatal is in the maintenance of updates brought trouble, need to constantly compile all do not say, and the code in a large number of comments, after a period of time to read is also a heavy mental work. And the loss of C concise and clear and efficient, and not fully equipped with C + + object-oriented features. This cannot be called C + + program at all. (I would like to have time to rewrite the previous code will also have a lot of harvest, warm so know new) C and C + + in the programming mentality is contradictory. This means that if you want to learn C + +, you can not learn C, just need a good book and a Not too stupid brain plus efforts on it, if you already have C experience under certain circumstances will be disruptive.

This chapter is a cursory view of the object model. Now that we have chosen C + + instead of C as a development tool, our programming ideas should also be converted to C + +, and we cannot extend the procedural way. We must learn how to think in C + +. Using the abstract data type or using a multi-layer class system to encapsulate data and processing functions, only get rid of the inertia of the C program's use of global data, can give full play to the powerful power of C + + object model.

There are two types of data members static and nonstatic in C + +, and three member functions static, nonstatic, and virtual. The C + + object model optimizes memory space and access time, nonstatic data members are placed inside class objects, and static data members are placed outside the class object. Static and nonstatic member functions are placed outside the class object. The virtual function is supported by a pointer to the VTBL (virtual function table) of a class object vptr. The vptr settings and resets are automatically completed by the constructors, destructors, and copy assignment operators of the class.

Almost every class we design has one or more constructors, destructors, and a assignment operator. Their role is that the constructor creates a new object and determines that it is initialized. The destructor destroys an object and determines that it has been properly cleaned (avoiding memory leaks), and the assignment operator gives the object a new value.

This is the first part of Chapter one, because Thor has been working on the homepage of the mode group for the last few days, and the time is running out. I want to finish the whole chapter again, think about it or send a part of it. The reason is 2. 1, the second half of the first chapter may be dragged for 10 days and half months. 2, notes are difficult to write, I do not want to make notes will be the focus of the book to copy one side, but like as much as possible to describe their own understanding, who knows the first chapter is so difficult to digest, has repeatedly read 3 times, or some half-cooked. So in the attitude of being responsible to everyone and himself, Thor is going to look at it 3 times. Suddenly found their own C + + is still poor far, so terrible ah.

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.