C ++ Review of member function overloading, class and struct, and this pointer

Source: Internet
Author: User

As mentioned in the previous article, inline member functions can be defined in two ways: one is to define them directly within the class body, and the other is to define the class in vitro. The following code is used:

The first definition method:

Class Test {public: void add (int a, int B) // inline function when directly defined in the class body. {Return a + B ;}};
The second definition method is as follows:

Inline Test: add (int a, int B) // defines {return a + B in vitro ;}
The purpose of inline functions is to improve program execution efficiency. during compilation, the compiler will embed the Code directly into the called place, thus reducing the overhead of function calls, if you directly embed the code without using inline functions, the size of the Code will increase. This is actually a policy of exchanging space for time. Therefore, the code of the inline function must be short. The inline function is only a prompt for the compiler. In addition, if the function contains statements such as switch for, the compiler will not interpret it as inline.

Next, let's talk aboutOverload of member functions

The reload of member functions is conditional.With the same scope and different parameters(It can be that the parameter type is different, the number of parameters is different, and the parameter order is different ),Same Function Name. Such a member function constitutes an overload. The following is an example of heavy load:

Class Test {public: void Init (); void Init (int x, int y); void Init (int x, int y, int z ); // void Init (int x = 0, int y = 0, int z = 0); // functions with void Init () generate ambiguity, the compiler cannot be distinguished };
In the above example, multiple Init functions are defined. Because their parameters are different, they constitute an overload. However, you must note that, to overload member functions with default values, you must avoid ambiguity, as shown in the comments line in the above Code. It is not distinguished from the member function Init () without parameters. Therefore, an error is returned.


Class and struct (struct and class)

The struct in C is actually a bit similar to the class in C ++. They can all close various data structures and methods in a container. The only difference is:When no access permission is specified, struct is public by default, while class is private by default.As follows:

class Test{    int x; //actually x is private.};
struct Test{    int x; //x is public};
You can define functions in class. In fact, you can also define functions in struct in C ++, but their access permissions are all public, if all data members and member functions defined in the class are public, there is no difference with the struct definition. The following code is very simple and you can see the difference between the two.

Struct Test2 {int x _; int y _; int z _; void Init (int x, int y, int z) {x _ = x; y _ = y; z _ = z ;}}; class Test3 {int x _; int y _; int z _; void Init (int x, int y, int z) {x _ = x; y _ = y; z _ = z ;}}; int main () {Test2 t; // in C ++, struct is also a class, no need to add struct t. init (10, 20, 30); Test2 t2 = {10, 20, 30}; // initialize the struct in C, which is Test3 t3; t3.Init (10, 20, 30 ); // Error, Init is private in t3 and cannot be directly accessed from outside Test3 t4 = {10, 20, 30}; // Error In the class, the data member is private and cannot be initialized like struct .}

About this pointer

To understand the this pointer, you need to understand the background: In C ++, the code of the member function is shared for all objects, and the data member space of each object is independent, so when every object calls a member function, how does the member function differentiate the data of which object should it operate? In other words, how does a member function know that it wants to operate on the data member of the object that calls it? This is mainly done by the C ++ compiler,The C ++ compiler adds an object pointer to the first parameter each time a member function is called to indicate the data of the object to be operated by the member function. The pointer passed by the compiler is invisible and implicit..

The following code calls a member function:

Test t1;t1.Init(10,20,30);

After processing by the compiler, it is like this:

Test t1;t1.Init(&t1, 10, 20, 30);
The pointer passed by the compiler is the this pointer, which points to the data member t1 when the member function code is executed. Of course, we can omit this pointer when defining a member function.



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.