C++ | 对象指针和对象数组

应用举例:

 1 #include<iostream>
 2 using namespace std;
 3 class Point{
 4     public :
 5         Point(){
 6         }
 7         Point(double x,double y):x_(x),y_(y){
 8         }
 9         Point &operator=(const Point &point){
10             if(&point == this)
11                 return *this;
12             
13             this->x_ = point.x_;
14             this->y_ = point.y_;
15             return *this;
16         }
17         print(){ 
18             cout<<"x:"<<this->x_<<" y:"<<this->y_<<endl; 
19         } 
20     private:
21         double x_;
22         double y_;
23 };
24 int main(){
25     Point points[4];
26     points[0] = Point(3,4);
27     points[1] = Point(5,6);
28     points[2] = Point(7,8);
29     points[3] = Point(1,8);
30     
31     //指针对象数组 
32     Point* p = points;
33     for(int i = 0; i < 4; i++)
34         (p+i)->print();
35         
36     //单个指针对象
37     Point *p1 = &points[0];
38     p1->print();
39 }

 

posted @ 2019-07-01 14:14  听说这是最长的名字了  阅读(780)  评论(0编辑  收藏  举报