Two types of C ++ Data Pointer

Source: Internet
Author: User

In the C ++ language, pointer applications also occupy a very important position. We used to briefly introduce the related concepts of function pointers. Here we will introduce the related concepts of C ++ data pointers, let friends have a more detailed understanding of the concept of pointer.

C ++ data pointers are classified into two types: regular data pointers and member data pointers.

Regular Data Pointer

This should not be clear. Like the C language, definition and assignment are simple and clear. Common examples include int * and double.
For example:

 
 
  1. int value = 123;  
  2. int * pn = &value; 

Member Data Pointer

It has the following structure:

 
 
  1. struct MyStruct  
  2. {  
  3. int key;  
  4. int value;  
  5. }; 

There is a structure object:

 
 
  1. MyStruct me;  
  2. MyStruct* pMe = &me; 

We need the address of the value member. We can:

 
 
  1. Int * pValue = & me. value;
  2. // Or
  3. Int * pValue = & pMe-> value;

Of course, this pointer is still the first model-the conventional C ++ Data Pointer.

Now we need a pointer that points to any data member in MyStruct, so it should be a child like this:

 
 
  1. Int MyStruct: * pMV = & MyStruct: value;
  2. // Or
  3. Int MyStruct: * pMK = & MyStruct: key;

This pointer is used to obtain the address of a structure member in the structure. We can use this pointer to access member data:

 
 
  1. Int value = pMe-> * pMV; // obtain pMe value member data.
  2. Int key = me. * pMK; // obtain the key member data of me.

So, under what circumstances will the C ++ Data Pointer of the member type be used? Indeed, member pointers are not commonly used. However, it is useful in some cases. Let's take a look at the following function:

 
 
  1. int sum(MyStruct* objs, int MyStruct::
    * pm, int count)  
  2. {  
  3. int result = 0;  
  4. for(int i = 0; i < count; ++i)  
  5. result += objs[i].*pm;  
  6. return result;  

What are the functions of this function? Its function is to calculate the sum of the given member data by giving a count pointer to the MyStruct structure. It's a bit difficult, right? Look at the following program and you may understand:

 
 
  1. MyStruct me [10] =
  2. {
  3. {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10}, {11, 12}, {13, 14 },
    {15,16 },{ 17,18 },{ 19,20}
  4. };
  5. Int sumsum_value = sum (me, & MyStruct: value, 10 );
  6. // Calculate the sum of the value members of 10 MyStruct structures:
    The sum_value is 110 (2 + 4 + 6 + 8 + 20)
  7. Int sumsum_key = sum (me, & MyStruct: key, 10 );
  8. // Calculate the sum of key members of 10 MyStruct structures:
    Sum_key is 100 (1 + 3 + 5 + 7 + 19)

Maybe, you think it can be done with regular pointers, and it is easier to understand. OK, no problem:

 
 
  1. int sum(MyStruct* objs, int count)  
  2. {  
  3. int result = 0;  
  4. for(int i = 0; i < count; ++i)  
  5. result += objs[i].value;  
  6. return result;  

Do you want to do this? In doing so, you can only calculate the value. If you want to calculate the key, you need to write another function. How many members need to be computed? How many functions do you need to write.

The concepts of C ++ data pointers are introduced here.

Related Article

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.