Basic knowledge of C ++ Program

Source: Internet
Author: User

Maybe you are not very familiar with the C ++ program. It will be helpful to you after reading this article, in addition to studying the basic nature of the C ++ program, we will also conduct a comprehensive study on the new features of the C ++ program, hoping to provide useful information.

 
 
  1. class  A  
  2.  
  3. {  
  4.  
  5. };  
  6.  
  7. main()  
  8.  
  9. {   
  10.  
  11. A  a;  
  12.  
  13.   cout<<sizeof(a)<<endl;  
  14.  

I have a lot of knowledge about C ++ programs. I have four libraries: C ++ programming and C Primer. I just want to talk about what I have seen. Add your own understanding. A lot of things may not be systematic, and the knowledge also comes to mind.

First, let's talk about the object size of the class,

 
 
  1.  class  A  
  2.  
  3. {  
  4.  
  5. };  
  6.  
  7. main()  
  8.  
  9. {   
  10.  
  11. A  a;  
  12.  
  13.   cout<<sizeof(a)<<endl;  

So:

 
 
  1. class A  
  2.  
  3. {  
  4.  
  5.   char m;  
  6.  
  7. };  
  8.  
  9. main()  
  10.  
  11. {  
  12.  
  13. A  a;  
  14.  
  15. cout<<sizeof(a)<<endl;  
  16.  

The number of printed results. This problem is called Alignment. Once, I encountered such a depressing problem. In our project, the Code uses arrays to transmit data, such as coordinates x and y. then assign values like this.

 
 
  1. *(int *)&w[0]=x;   
  2. *(int *)&w[4]=y; 

It may be rarely used). This code has a disadvantage: it is inconvenient to send data and retrieve data. In addition, due to the subscript, the code is not easy to use the loop structure and there are not four digits of data ). Only one value can be assigned by one digit. Another problem is that when a project Convention changes, all the Code must be changed. Bad reuse.

Therefore, I want to use the strcut structure or class to modify the program code, but I have encountered the Data Alignment problem.

For example, I define a C ++ program structure:

 
 
  1. struct A  
  2. {  
  3.  char type;  
  4. int y;  
  5. };  
  6. char  buf[MAX];   
  7. A a;  
  8. memcpy(buf,&a,sizeof(a));  
  9.  

However, three buf [] contents are not what I need. In fact, I have read some materials to say that this is for byte alignment, and the CPU access data is based on four-bit access, so that it will not take four-bit data, one is char type, three digits are y data. Programmers may not be familiar with this.

There are other similar problems. We will discuss them later. Class data access permission data has three permissions: public, private, and protected. C ++ is an object-oriented language. Therefore, many of its concepts come from real life. For example, this permission issue can be explained in this way. The basic class is equivalent to the father, and the Child class is equivalent to the Child class.

There is also a friend Meta class, which is equivalent to a friend. For example, it is not a secret to share all the variables that the father has. So, use public to describe such a thing. If something is not known to outsiders.

That is, if you can share secrets with your son and friends, you can use protected to describe such things. Similarly, there are other things that adults don't want their children to know, but they don't keep their friends. Such things are also available in real life, so they use private descriptions.

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.