Summary of pointer usage in C + +

Source: Internet
Author: User
Tags constant valid

In C + +, the pointer used more, for many of the pointer usage, unavoidably make some confused, since I borrowed a book from the library Tan Haoqiang Professor Editor of the "C + + object-oriented program design," To help me clear the train of thought. Now part of the content to show to everyone, I hope to help you.

The important basis of C + + programming is class and object, object pointer is a very important part, including pointer to object, pointer to object member, this pointer, constant pointer to object, pointer to constant object, etc.

  1, pointing to the object of the pointer

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

Note: When creating objects, compiling the system allocates a certain amount of storage space for each object to hold its members, but note that in general, the data stores in different objects are not the same, while the function codes of the different objects are the same, that is, their function code is shared. Then we can define a pointer variable to hold the object.

The general form of a pointer variable that defines a class object is:

Class Name * object pointer name;

For objects with a time class, we can have:

Time t;

Time *p;

p=&t;

We can access objects and members of objects through object pointers, if the defined class has data members hour, minute, SEC, member functions have gettime (), then

(*p). Hour is the hour member of the object, equivalent to the T.hour

(*p). gettime () is P pointing to the member function gettime () in the object, equivalent to T.gettime ()

You can also use the following form:

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

  2, pointing to the object member's pointer

(1) Pointer to object data member

In c we have learned pointer variables that point to ordinary variables, and in C + + the method for defining pointer variables to object data members is the same as defining a pointer variable method that points to a generic variable, in the general form:

Data type name * pointer variable name;

Such as:

int *p;

p=&t.hour; Assigns the address of the data member hour of the object T to the p,p point T.hour

(2) Pointer to an object member function

Defines a pointer variable that points to an object member function and defines a different pointer variable that points to a normal function.

When defining a pointer variable that points to a normal function, we can define this:

void (*p) ();

P=fun;

(*p) (); Calling the fun function

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

function parameter type and number to match

Type of function return value to match

To match the class to which it belongs

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

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

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

void (time::* p) (); To define a pointer variable p that points to a time class object member function

p=&time::gettime; Assign the public member function gettime () address of the time class to the pointer variable p

(t.*p) (); Call the member function of the time class object T gettime ()

Note: Because member functions are not stored in object space, multiple objects of the same type share this member function code, so assigning a member function's entry address to a pointer variable should be written as:

Pointer variable pointing to an Object member function =& class name:: Name of member functions;

There is no "()" after the member function name, if written as P=&time::gettime () is wrong. [NEXTP

  3. This pointer

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

such as defining a function that asks for volume

int Box::vol ()

{return (height*width*length);}

If the object T is already defined, when the member function T.vol () is invoked, the compilation system assigns the start address of the object T to the this pointer, so that when the member function references the data member, the data member of the object T can be referenced according to the pointer. So C + + handles 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 on which the member function is currently invoked, you can write

int Box::vol ()

{return ((*this). height* (*this). width* (*this). length);

So when the member function T.vlo () is invoked, the actual invocation is T.vol (&t), but the address of the object T to the this pointer is automatically completed by the system and is not artificially added.

  4, pointing to the object's regular pointer

Declare a pointer variable to an object as a const and start at the beginning, so that the pointer value is always kept at its original value and cannot be changed to its point.

Such as:

Time T1 (8,8,8), T2;

Time *const p=&t1; Constant pointer p points to object T1

p=&t2; Trying to change the point 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 = object's starting address;

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

A constant pointer is generally used as an argument to a function, so that the value of the pointer variable is not allowed to be changed during function execution so that it remains pointed to the original object.

  5, pointing to the constant object pointer variable

First, look at the pointer variable that points to the constant variable, and its general form is:

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 pointing to the constant variable, not with a generic pointer variable. In addition, pointer variables that point to constant variables can point to variables that are not declared as const, but cannot change their values through the pointer. For example:

Char c= ' a '//define character variable C, not known as const

const char *p; Defines the pointer variable p for a constant variable

p=&c; P point to character variable C

*p= ' B '; Illegal

C= ' B '; Legal

Note: In the example above, the pointer variable p points to the character variable C, does not say that C is also declared as a constant variable, but only that in reference to C through the pointer variable, C has the characteristics of the constant variable, the value is immutable, but C is still a normal variable.

In pointers to constant variables, there are several points to note about the type of pointer to a function parameter:

If a function parameter is not a const pointer variable, the argument can only be directed to a non-const pointer, and if the function parameter is a pointer to a const variable, the argument can be either a const or a const-type pointer variable. In other words, pointers to constant variables can point to const and non-const variables, whereas pointers to non-const variables can only point to variables that are not const.

the corresponding table

of formal parameters and arguments with pointer variables

const variable is
parameter argument legal or not
address of a non-const variable > legal row
pointer to non-const variable illegal /
const variable's address legitimate no
point to the const type variable Needle non-const changeThe amount of the address "> valid > no

Here's the point, which is similar to the pointer variable for the constant object and to the pointer variable of the constant variable.

If an object has been declared as a long object, you can only point to it with a pointer variable that points to a literal object, rather than pointing to it with a generic pointer variable.

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

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

Such as:

Time T1 (8,8,8), T2; Defining objects

Const TIME *p=&t1; Defines the pointer p for a constant object and points it to the T1

p=&t2; Valid, pointer p points to object T2

So when we want the value of the object not to be modified when we call the function, we can define the parameter as a const pointer variable and use the address of the object as an argument (the object can be either a const or a const type), and when the request object is not changed not only in the calling function, And it doesn't change during the execution of the program, we define it as a const type. Therefore, pointers to regular objects are most commonly used in parameters of functions to protect the object pointed to by the parameter pointer from being modified during function execution.

The above is My Learning C + + pointer when the record, but also the use of C + + pointer usage Summary, I want to learn C + + should be very helpful.

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.