Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pointers in C++

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use pointers in C++". In daily operation, I believe many people have doubts about how to use pointers in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to use pointers in C++". Next, please follow the editor to study!

The important foundation of C++ programming is classes and objects, and object pointers are a very important part, including pointers to objects, pointers to object members, this pointers, constant pointers to objects, pointers to constant objects, and so on.

1. A pointer to an object

Definition: the starting address of the object space is the pointer to the object.

Description: in the establishment of objects, the compiler system allocates a certain storage space for each object to store its members, but note that in general, the data members stored in the data storage units of different objects are different, while the function codes of different objects are the same, that is to say, their function codes are shared. At this point, we can define a pointer variable to hold the pointer to the object.

The general form of defining a pointer variable to a class object is:

Class name * object pointer name

For example, for a Time class object, we can have:

Time t; Time * p; pendant;

We can access the object and its members through the object pointer. If the defined class has data members hour, minute, sec, and the member function has gettime (), then

(* p) .hour is the hour member in the p pointing object, which is equivalent to t.hour

(* p) .gettime () means that p points to the member function gettime () in the object, which is equivalent to t.gettime ().

It can also be in the following form:

P-> hour and p-> gettime () are equivalent to the above.

2. Pointers to object members

(1) pointers to object data members

We learned about pointer variables to ordinary variables in C #. The method of defining pointer variables to object data members in C++ is the same as defining pointer variables to ordinary variables. The general form is as follows:

Data type name * pointer variable name

Such as:

Int * p; pdistribut.hour. source; / / assign the address of hour, the data member of object t, to PBook p pointing to t.hour.

(2) pointers to object member functions

Defining a pointer variable to an object member function is different from defining a pointer variable to an ordinary function.

When defining a pointer variable to an ordinary function, we can define it as follows:

Void (* p) (); pairfunn; (* p) (); / / call the fun function

However, the compilation system requires that three conditions must be met when assigning a function address to a pointer variable:

The type and number of function parameters should be matched

The type of the return value of the function should match

The class to which it belongs should match.

Obviously the p above has nothing to do with the class. In order to satisfy the third item, we can specify a class for the pointer, so the pointer variable to the object member function is generally in the form of:

Data type (class name:: * pointer variable name) (parameter table column)

You can point the pointer to a common member function, such as:

Void (Time::*p) (); / / defines the pointer variable to the member function of the Time class object, p _ pointer _ gettime (); / / assigns the address of the public member function gettime () of the Time class to the pointer variable p (t.roomp) (); / / calls the member function gettime () of the Time class object t.

Note: because the member function is not stored in the object space, and multiple similar objects share this member function code, the entry address of the member function should be assigned to the pointer variable as:

Pointer variable to object member function = & class name:: member function name

There is no "()" after the member function name, and it would be wrong to write p=&Time::gettime (). [nextp

3. This pointer

Each member function contains a special pointer called this, which is a pointer to an object of this class, and its value is the starting address of the object where the member function is currently called. The reason for this pointer is to ensure that the member functions of different objects of the same kind refer to the data members in the specified object, which is automatically implemented by the system.

Such as defining a function that calculates volume.

Int box::vol () {return (height*width*length);}

If the object t has been defined, when calling the member function t.vol (), the compiler assigns the starting address of the object t to the this pointer, so when the member function references the data member, it can refer to the data member of the object t according to the pointer this. So C++ processes the above function as

Int box::vol () {return (this- > height*this- > width*this- > length);}

Because the value of this is the starting address of the object where the member function is currently called, it can be written as

Int box::vol () {return ((* this) .height* (* this) .width* (* this) .length);}

So when calling the member function t.vlo (), the actual calling method is t.vol (& t), but the address of the object t is passed to the this pointer automatically by the system, without artificial addition.

4. A constant pointer to an object

Declare and initialize the pointer variable to the object so that the pointer value remains at its initial value and cannot change its direction.

Such as:

Time T1 (8 const 8), T2; Time * paired pointer T1; / / constant pointer p points to the object T1 paired pointer T2; / / attempts to change the direction of p, illegal

From the above, we can see that the general form of defining a constant pointer to an object is:

Class name * const pointer variable name = the starting address of the object

Note: the value of a constant pointer variable to an object cannot be changed, but you can change the value of a data member in the object it points to.

The constant pointer is generally used as an argument to the function, so it is not allowed to change the value of the pointer variable during the execution of the function so that it always points to the original object.

5. Pointer variables to constant objects

First, review the pointer variables that point to constant variables, which are generally in the form of:

Const type name * pointer variable name

Such as:

Const char * p

If a variable has been declared as a constant, it can only be pointed to by a pointer variable that points to a constant variable, not a normal pointer variable. In addition, a pointer variable to a constant can point to a variable that is not declared as const, in addition to a constant, but cannot change its value through the pointer. For example:

Char / defines the character variable c, which is not known as const const char * p; / / defines the pointer variable to the constant variable, p / p, which points to the constant variable; / / p points to the character variable, c * proomroombaked; / / illegal cantilevered variables; / / legal

Explanation: in the above example, the pointer variable p points to the character variable c, which does not mean that c is also declared as a constant variable, but only that when referencing c through the pointer variable, c has the characteristics of a constant variable and its value cannot be changed, but c is still an ordinary variable.

Among pointers to constant variables, there are several points to note about the pointer types of function parameters:

If the function parameter is a non-pointer variable, the argument can only use a pointer to the non-pointer variable; if the function parameter is a pointer to a pointer variable, the argument can be a pointer variable of type or non-type. In other words, pointers to constant variables can point to const and non-const variables, while pointers to non-constant variables can only point to non-permanent variables.

The corresponding relation table of the formal parameter and the actual parameter when using the pointer variable as the parameter:

Whether the parameter argument is legal or not changing the value of the object referred to by the pointer to the address of the non-const variable, the legal row pointing to the pointer to the non-const variable, the address of the const variable is illegal / the address of the pointer to the const variable is illegal, the address of the pointer to the variable is not legal, the address of the variable is not legal.

To get to the point, pointer variables that point to constant objects are similar to pointer variables that point to constant variables.

If an object is already declared as a long object, it can only be pointed to it with a pointer variable that points to a constant object, not a normal pointer variable.

If you define a pointer variable to a constant object and make it point to a non-const object, the object it points to cannot be changed by a pointer.

If you define a pointer variable that points to a constant object, you cannot change the value of the object it points to, but the value of the pointer variable itself can be changed.

Such as:

Time T1, T2; / / define the object const time * paired pointer T1; / / define a pointer p to a constant object and make it point to T1 pendant roomT2; / / legally, the pointer p points to the object T2

So when we want the value of the object not to be modified when calling the function, we can define the parameter as the pointer variable of the const type, and use the address of the object as the argument (the object can be of the same type or not); when it is required that the object is not changed not only in the calling function, but also during the execution of the program, we define it as the pointer type. Therefore, pointers to constant objects are most commonly used in function parameters to protect the object pointed to by the parameter pointer from being modified during function execution.

At this point, the study of "how to use pointers in C++" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report