How C + + member functions are stored---11

Source: Internet
Author: User

Original blog: Reprint please indicate the source: http://www.cnblogs.com/zxouxuewei/

member functions belong to members of a class and appear in the re-class body. Can be designated as public, private, or protected.

1. When defining member functions outside of a class, you need to use the class name with the scope qualifier (::), as in the following code:

#include <iostream>#include<string>using namespacestd;class_test_myclass{ Public:    voidDisplayConst int&value); Declare member functions within a class}class1;void_test_myclass::d isplay (Const int&value)//define member functions outside the class, with the scope operator {cout<<"value is"<<value<<Endl;}intMainintargcConst Char*argv) {Class1.display ( -); return 0;}

2.inline member functions

The inline member function is an introverted function (also called a built-in function), and member functions defined within a class can be used without the inline keyword. Because the in-class member function defaults to built-in functions.

#include <iostream>#include<string>using namespacestd;class_test_myclass{ Public:    voidDisplayConst int&value)//In-class member function without adding the inline keyword {cout<<"value is"<<value<<Endl; }}class1;inlinevoidDisplay2 (Const int&value)//declared as built-in function {cout<<"value is"<<value<<Endl;}intMainintargcConst Char*argv) {Display2 ( -); return 0;}

3. How member functions are stored

When a class is used to define an object, the system allocates storage space for each object. If a class includes data and functions, allocate storage space for the code for the data and functions, respectively.

Logically, if 10 objects are defined with the same class, then the data and function code for 10 objects will be allocated to the storage unit, as shown in 8.4.

Figure 8.4

Can you only use a space to store this common function code snippet, and call the common function code when invoking the functions of each object. As shown in 8.5.

Figure 8.5

Obviously, this will save storage space significantly. This is what the C + + compilation system does, so each object consumes only the storage space occupied by the data portion of the object, not the storage space that the function code occupies. If a class is declared:

class time{  public:  int  hour;   int minute;   int sec;   void Set ()  {      cin>>a>>b>>C;  }};

You can use the following statement to output the number of bytes that the class object occupies:

  cout<<sizeof(time) <<endl;

The value of the output is 12.

This proves that the amount of space an object occupies depends only on the space occupied by the data members in the object, regardless of the member function.

The function code is stored outside of the object space. If you define 10 objects for the same class, the member functions of those objects correspond to the same function code snippet, not 10 different function code snippets. It is important to note that although the member functions of different objects are called to execute the same function code, the execution results are generally not the same.

Different objects use the same function code snippet, how can they manipulate the data in different objects separately?
The original C + + for this purpose set up a pointer named this, to point to different objects. Need to explain:
The code snippets for member functions are stored in the same way, regardless of whether member functions are defined within a class or outside the class.
Do not confuse this method of storing member functions with the concept of inline (built-in) functions.
It should be noted that the "member function of a certain object" is often said to be from a logical point of view, and the method of storing member functions is from the physical point of view, the two are not contradictory.

How C + + member functions are stored---11

Related Article

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.