Talking about Objects and Objects in C ++

zhaozj2021-02-11  237

Column: VC Camp - C / C Column | Join Date: 2001-6-30 17:08:44 | Reading: 162 Close window object pointer and object reference

Pointer to the member of the class

In C , you can explain the pointer to the data member of the class and the member function.

The pointer format points to the data member is as follows:

:: *

The pointer format points to the member function is as follows:

( :: * ) ()

For example, there is a class A:

Class A

{

PUBLIC:

Int fun (int b) {RETURN A * C B;}

A (INT I) {a = i;}

INT C;

Private:

Int a;

}

Define a pointer PC pointing to the data member C of class A. The format is as follows:

INT a :: * pc = & a :: c;

A pointer PFUN that points to the member function of class A is again defined, and its format is as follows:

Int (a :: * pfun) (int) = a :: fun;

Since the class is not an object that exists while running. Therefore, when using this type of pointer, you need to first specify an object of the Class A, and then reference the members pointed to by the pointer. For example, the data member C pointed to the PC pointer is 8, which can be represented as follows:

A a a;

a. * pc = 8;

Among them, the operator. * Is an object used to operate the class to the pointer to the class member.

Use the operator-> * when the pointer to the object is used. E.g:

A * P = & a; // A is an object of class A, P is a pointer to object A.

P -> * pc = 8;

Let's take a look at the definition format of a pointer to the general function:

* ()

The format of the pointer to the pointing function is as follows:

=

About the format of the pointer call function using the pointing function in the program is as follows:

(* ) ()

If it is a pointer to the member function of the class, the corresponding object name and object member operator should be added.

The following is given an example of using a member pointer:

#include

Class A

{

PUBLIC:

A (INT I) {a = i;}

Int fun (int b) {RETURN A * C B;}

INT C;

Private:

Int a;

}

void main ()

{

A x (8); // Define an object X of class A

INT A :: * PC; // Defines a pointer PC pointing to class data.

PC = & a :: c; // assign a value to the pointer PC

x. * pc = 3; // assigns a value to class member C with a pointer.

INT; // Defines a pointer Pfun pointing to class member functions

PFUN = a :: fun; // Assign the pointer PFUN

A * p = & x; / / Define an object pointer P and assigns first value X

COUT << (p -> * pfun) (5) << endl; // Use object pointer adjustment to the function of PFUN Pointing Pfun

}

The above programs define several pointers, although they are all pointers, but the objects pointed out are different. P is an object to the class; the PC is a data member that points to the class; PFUN is a member function that points to the class. Therefore, their values ​​are also different. Parameters for object pointers and object references

1. Parameters of the object pointer function

Use the object pointer as a function parameter to use the object to make functions parameters more common. Because the use of object pointers, the function parameters have the following two points:

(1) Realize the service call. The value of the parameter object of the call function can be changed in the called function, and the information transfer between the functions can be implemented.

(2) Use the object pointer to the ginseng to conveys the address value of the object, without copying copy, which can improve the operational efficiency and reduce time and space overhead.

When the parameter is directed to the object pointer, the corresponding argument of the call function should be the address value of an object, generally use & a rear object name. The following example will illustrate the object pointer function parameters.

#include

Class M

{

PUBLIC:

M () {x = y = 0;}

M (INT I, INT J) {x = i; y = j;}

Void Copy (m * m);

Void Setxy (INT I, INT J) {x = i; y = j;}

void print () {cout << x << "," << Y << endl;}

Private:

INT X, Y;

}

Void M :: Copy (m * m)

{

X = m-> x;

Y = m-> y;

}

Void Fun (M m1, m * m2);

void main ()

{

M p (5, 7), q;

Q.copy (& P);

Fun (p, & q);

p.Print ();

q.print ();

}

Void Fun (M M1, M * M2)

{

M1.setxy (12, 15);

M2-> setxy (22, 25);

}

The output is:

5,7

22, 25

As can be seen from the output, when the function is called in the function function, the data member value of the object is changed [M1.Setxy (12, 15)] and the data member value pointing to the object pointer [M2-> setxy (22, 25) Later, it can be seen that only objects pointed to the object pointer are changed, while the other object is parameter, the value of the parameter target value changes, and the actor value value does not change. Therefore, the above result is output.

2. Object reference work function parameters

In practice, the use of object references function parameters is more common than using the object pointer, because the use of object reference function parameters has the advantages of using the object pointer to function parameters, and the object reference to function parameters will be simpler, more directly. Therefore, in C programming, people like to use object reference to function parameters. An example of an example of an object reference to function parameters.

#include

Class M

{

PUBLIC:

M () {x = y = 0;}

M (INT I, INT J) {x = i; y = j;}

Void Copy (M & M);

Void Setxy (INT I, INT J) {x = i; y = j;}

void print () {cout << x << "," << Y << endl;}

Private:

INT X, Y;

}

Void M :: Copy (M & M)

{

x = m.x;

x = m.y;

}

Void Fun (M M1, M & M2);

void main ()

{

M p (5, 7), q;

Q.copy (p); fun (p, q);

p.Print ();

q.print ();

}

Void Fun (M M1, M & M2)

{

M1.setxy (12, 15);

M2.setxy (22, 25);

}

This example is output the same as the above example, but the parameters when the call is called.

THIS pointer

The THIS pointer is a special pointer hidden in each member function. It is an object that is pointed to being operated by the member function, that is, to operate the member function.

When calling a member function to an object, the compiler first assumes the address of the object to the THIS pointer, then calls the member function, and each member function accesses the data member, this pointer is hidden. And usually do not explicitly use the THIS pointer to reference data members. You can also use * this to identify objects that call the member function. The following example shows the application of the THIS pointer.

#include

Class A

{

PUBLIC:

A () {a = b = 0;}

A (INT I, INT J) {a = i; b = j;}

Void Copy (A & AA); // Object Reference Working Function Parameters

Void Print () {cout << a << "," << b << endl;

Private:

INT A, B;

}

Void A :: Copy (A & AA)

{

IF (this == & aa) return; // This this is the address of the object of the member function, here is the address of the object A1

* this = aa; // * This is an object to operate the member function, here is an object A1.

// This statement is an object AA assigned to A1, that is, the value of the data member of the AA is assigned to the data member of the A1.

}

void main ()

{

A A1, A2 (3, 4);

A1.Copy (A2);

A1.Print ();

}

operation result:

3, 4

2001-7-6 23:56 Aqing

转载请注明原文地址:https://www.9cbs.com/read-3810.html

New Post(0)