C ++ pointer: data member pointer

Source: Internet
Author: User

I haven't been learning C ++ for a long time. I have forgotten some of the things I learned before, and now I have forgotten some things. I think of them occasionally. By the way, let's write them down. When I think about it next time, it takes more than half a day to remember.

C ++ pointers can be divided into two types: Data pointers and function pointers. Data pointers include common data pointers and member data pointers. function pointers also include common function pointers and member function pointers. In the process of learning C ++, most of the pointer problems first come into being are common data pointers. This type of pointer is a lot of pointers that are used in comparison and easier. Of course, this is a "very difficult" thing. I don't really have the best pointer skills. That is to say, if you learn a little bit and collect more data, you will naturally know more. More exercises will naturally become more familiar.

Let's get down to the point. First, let's give an example of the data member pointer:

 
1: # include"Stdafx. h"
 
2: # include"Father. h"
3: # include"Child. h"
 
4: # include <vector>
 
5:
 
6:StructPoint
 
7 :{
8:IntX;
 
9:IntY;
 
10:DoubleD;
 
11:DoubleD2;
 
12 :};
13:
 
14:IntAdd (point arr [],IntSize,IntPoint: * P)
 
15 :{
 
16:IntSum = 0;
17:For(IntI = 0; I <size; I ++)
 
18 :{
 
19: Sum + = arr [I]. * P;
 
20 :}
 
21:ReturnSUM;
22 :}
 
23:
 
24:DoubleAdd1 (point arr [],IntSize,DoublePoint: * P)
 
25 :{
26:DoubleSum = 0;
 
27:For(IntI = 0; I <size; I ++)
 
28 :{
 
29: Sum + = arr [I]. * P;
 
30 :}
31:ReturnSUM;
 
32 :}
 
33:
 
34:Int_ Tmain (IntArgc, _ tchar * argv [])
 
35 :{
36: Point PP [3] = {1.0, 2.0, 1.0}, {2.0, 1.0, 2.0 }};
 
37:
 
38: cout <add (PP, 3, & point: X) <Endl;
 
39: cout <add (PP, 3, & point: Y) <Endl;
 
40:
41: cout <Add1 (PP, 3, & point: d) <Endl;
 
42: cout <Add1 (PP, 3, & point: D2) <Endl;
 
43: System ("Pause");
 
44:Return0;
45 :}

This sectionProgramA simple function is executed. First, the point structure has four data members. The add function accumulates all the data values of the specified int type members in the point. The Add2 function accumulates the values of all data of a specified Member of the double type in the point.

It gives me the feeling that the pointer of a data member is a bit like an inherited peptide, and it is a dynamic binding process. When defining a function, you do not know the objects to be accumulated, but only know the member type. During the call, the specific member is specified.

I don't know if my understanding is biased. I hope the experts who passed by can give some advice and make common progress!

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.