Home>

c++ object pointer

Pointer to object

When creating an object,The compilation system allocates a certain amount of storage space for each object.To store its members.The starting address of the object space is a pointer to the object.You can define a pointer variable,A pointer to hold the object.

If there is a class:

class time
{
  public:
  int hour;
  int minute;
  int sec;
  void get_time ();
};
void time ::get_time ()
{
  cout<hour<<":<minute<<":"<sec<endl;
}

On this basis there are the following statements:

 time * pt;//Define pt as a pointer variable to a time object
  time t1;//Define t1 as a time object
  pt =&t1;//Assign the starting address of t1 to pt

In this way, pt is a pointer variable to a time object,It points to object t1.

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

 Class name * object pointer name;

Objects and their members can be accessed through object pointers.Such as:

 * pt //pt points to the object,T1
  (* pt) .hour //The hour member in the object pointed to by pt, that is, t1.hour
  pt->hour //The hour member in the object pointed to by pt, that is, t1.hour
  (* pt) .get_time () //Call the get_time function in the object pointed to by pt, that is, t1.get_time
  pt->get_time () //Call the get_time function in the object pointed to by pt, that is, t1.get_time

The effect of lines 2 and 3 above is equivalent,Lines 4 and 5 are also equivalent.

Pointer to object member

The object has an address,The pointer variable that stores the initial address of the object is the pointer variable that points to the object.The members in the object also have addresses,A pointer variable that stores the address of an object member is a pointer variable that points to an object member.

1) pointer to object data member

You define pointer variables to object data members in the same way as you define pointer variables to ordinary variables.E.g:

 int * p1;//Define a pointer variable to integer data

The general form of defining a pointer variable to an object data member is:

 Data type name * pointer variable name;

If the data member hour of the time class is a public integer data,The object data member hour can be accessed outside the class through a pointer variable to the object data member:

 p1 =&t1.hour;//Assign the address of data member hour of object t1 to p1, p1 points to t1.hour
  cout<<* p1<endl;//output the value of t1.hour

2) pointer to object member function

The reader should be reminded that the method of defining pointer variables to object member functions is different from the method of defining pointer variables to ordinary functions.Here is a review of the definition of a pointer variable to a normal function:

Data type name (* pointer variable name) (parameter table column);

Such as

void (* p) ();//p is a pointer variable to a void function

Can make it point to a function,And call the function via a pointer variable:

p=fun;//Pass the population address of the fun function to the pointer variable child p, and p points to the function fn

(* p) ();//call fn function

Defining a pointer variable to an object member function is more complicated.If you imitate the above method and assign the object member function name to the pointer, it becomes the most p:

 p=t1.get_time;

A compilation error will occur.why?

A member function has one of the most fundamental differences from ordinary functions:it is a member of a class.The compilation system requires that in the assignment statement above,The type of the pointer variable must match the type of the function to the right of the assignment number,It is required to match in the following 3 aspects:

① the type and number of function parameters;

② the type of function return value;

③ The class to which it belongs.

Now the first two points of the three points match.The third point does not match.The pointer variable p has nothing to do with the class,The get_time function belongs to the time class. Therefore, to distinguish the different properties of ordinary functions and member functions,You cannot call a member function directly outside the class with the member function name as the function entry address.

So, how should you define a pointer variable to a member function?It should take the following form:

 void (time ::* p2) ();//Define p2 as a pointer variable to a public member function in the time class

Note:The brackets on both sides of (time ::* p2) cannot be omitted,Because () has higher priority than *. Without this parenthesis,Is equivalent to:

 void time ::* (p2 ()) //This is a function that returns a void pointer

The general form of defining a pointer variable to a public member function is:

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

You can make it point to a public member function,Just assign the entry address of the public member function to a pointer variable to the public member function.Such as:

 p2 =&time ::get_time;

The general form of making a pointer variable point to a public member function is

 Pointer variable name =&class name ::member function name;

In vc++ system, you can not write&to be consistent with the usage of c language.But it is recommended not to omit&when writing c++ programs.

[Example] How to use the object pointer.

#include<iostream>
using namespace std;
class time
{
  public:
  time (int, int, int);
  int hour;
  int minute;
  int sec;
  void get_time ();
};
time ::time (int h, int m, int s)
{
  hour=h;
  minute=m;
  sec=s;
}
void time ::get_time () //Declares public member functions
//Define public member functions
{
  cout<hour<<<><minute<<
}
int main ()
{
  time t1 (10,13,56);//define time class object t1
  int * p1 =&t1.hour;//Define a pointer variable p1 to integer data, and make p1 point to t1.hour
  cout<<* p1<endl;//output the data member pointed to by p1 t1.hour
  t1.get_time ();//Call the member function of object t1 get_time
  time * p2 =&t1;//Define the pointer variable p2 to the time class object, and make p2 point to t1
  p2->get_time ();//Call the get_time function of the object pointed to by p2 (that is, t1)
  void (time ::* p3) ();//Define a pointer variable p3 to a public member function of the time class
  p3 =&time ::get_time;//Make p3 point to public member function get_time of time class
  (t1. * p3) ();//Call the member function pointed to by p3 in object t1 (i.e. t1.get_time ())
  return 0;
}

The program running result is:

10 (output of main function line 4)
10:13:56 (output of line 5 of the main function)
10:13:56 (output of line 7 of the main function)
10:13:56 (output of line 10 of the main function)

It can be seen that in order to output the values ​​of hour, minute, and sec in t1, three different methods can be used.

A few notes:

1) As can be seen from line 9 of the main function,The correct way to write the entry address of a member function is:

 &Class name ::member function name

Should not be written as:

 p3 =&t1.get_time;//t1 is the object name

Member functions are not stored in the space of the object,It is stored in the space outside the object.If there are multiple objects of the same type,They share the same function code segment.Therefore, the pointer variable p3 should be the entry address of this common function code segment.

Calling the get_time function of t1 can be in the form of t1.get_time (), which is from a logical point of view,Member functions can be called by object names.And now the address is needed in the program statement,It is physical,Specific addresses are associated with classes, not objects.

2) The eighth and nineth lines of the main function can be combined into one line:

 void (time ::* p3) () =&time ::get_time;//Specify the pointer variable when defining it

c++ object array

Arrays can be composed of not only simple variables (for example, each element of an integer array is an integer variable), but also objects (each element of an object array is a homogeneous object).

in daily life,There are many entities whose attributes are common,Only the specific content of the attribute is different.For example, a class has 50 students, and the attributes of each student include name, gender, age, grade, and so on. If you create an object for each student,Need to take 50 object names.It's inconvenient to programmatically.You can define an array of "student class" objects,Each array element is a "student class" object. E.g

 student stud [50];//Assuming that the student class has been declared, the stud array is defined with 50 elements

When building the array,The constructor is also called.If there are 50 elements, the constructor needs to be called 50 times.

When needed, you can provide arguments to define the array for initialization.If the constructor has only one parameter,When defining an array, you can provide arguments directly in curly braces after the equal sign.Such as

student stud [3]={60,70,78};//Legal, 3 arguments are passed to the constructor of 3 array elements, respectively

If the constructor has multiple parameters,You cannot use a method that provides all arguments directly when defining an array,Because an array has multiple elements,Provide multiple arguments for each element,If we consider the case where the constructor has default parameters,It is easy to cause the corresponding relationship of actual participation parameters to be unclear.Ambiguity appears.For example, the constructor of class student has multiple parameters,And is the default parameter:

student ::student (int=1001, int=18, int=60);//Define the constructor,Has multiple parameters,And is the default parameter

If the statement defining the array of objects is

 student stud [3]={1005,60,70};

It's best not to use this method which is easy to cause ambiguity in the program.

The compilation system only passes one argument to the constructor of each object element,Therefore, the number of actual parameters provided in the definition of the array cannot exceed the number of array elements.Such as

 student stud [3]={60,70,78,45};//Illegal, the number of actual parameters exceeds the number of object array elements

Well, if the constructor has multiple parameters,How should you initialize when defining an array of objects?The answer is:Write the constructor in braces and specify the arguments.

If the constructor has three parameters, they represent the student number, age, and grade. You can define an array of objects like this:

student stud [3]={//define object array
  student (1001,18,87), //Call the constructor of the first element,Provide it with 3 arguments
  student (1002,19,76), //Call the constructor of the second element,Provide it with 3 arguments
  student (1003,18,72) //Call the constructor of the third element,Provide it with 3 arguments
};

When building an array of objects,Call the constructor separately,Initialize each element.The arguments of each element are enclosed in parentheses,A set of parameters corresponding to the constructor,No confusion.

[Example] How to use the object array.

#include<iostream>
using namespace std;
class box
{
public:
  //Declares a constructor with default parameters,Initialize data members with parameter initialization tables
  box (int h=10, int w=12, int len ​​= 15):height (h), width (w), length (len) {}
  int volume ();
private:
  int height;
  int width;
  int length;
};
int box ::volume ()
{
  return (height * width * length);
}
int main ()
{
  box a [3]={//define object array
   box (10,12,15), //Call the constructor box and provide the argument of the first element
   box (15,18,20), //Call the constructor box and provide the argument of the second element
   box (16,20,26) //Call the constructor box and provide the argument of the third element
  };
  cout<"volume of a [0] is"<a [0] .volume ()<endl;
  cout<"volume of a [1] is"<a [1] .volume ()<endl;
  cout<"volume of a [2] is"<a [2] .volume ()<endl;
  return 0;
}

The results are as follows:

volume of a [0] is 1800
volume of a [1] is 5400
volume of a [2] is 8320
c
  • Previous jdbc operation mysql database instance
  • Next Detailed process of configuring java environment variables in Linux