1 #include <iostream> 2 #include<string> 3 using namespace std; 4 5 class Point { 6 public: 7 Point(int a, int b) :x(a), y(b) {}; 8 //Point(Point& p) :x(p.x), y(p.y) {}; 9 int getX() { return x; } 10 int getY() { return y; } 11 void print() { 12 cout << "x: "X << << endl << " Y: " << Y << endl; 13 is } 14 Private : 15 int X, Y; 16 }; . 17 18 is Point re_Ponit ( int A, int B) { . 19 Point RES (A , B); // create a local variable 20 is return RES; // pass the object entity 21 is } 22 is 23 is Point & re_PonitReference1 ( int a, int B) { 24 Point RES (a, B); // create a local variable 25 return RES; // pass object references (similar pointer) 26 is } 27 28 Point & re_PonitReference2 ( int A, int B) { 29 Point * RES = new new Point (A, B); // dynamic memory allocation 30 return * RES; / / transfer object reference 31 is } 32 33 is 34 is int main () 35 { 36 Point re_Ponit A1 = ( . 1 , 2 ); // copy constructor 37 [ Point & re_PonitReference1 A2 = ( 2 , . 3); // Assignment by reference (this function is passed a reference to a local variable) 38 is Point & re_PonitReference2 A3 = ( 2 , . 3 ); // reference assignment (the transfer function is a reference to a heap memory variable) 39 a1.print (); // 1,2 40 a2.print (); // random value 41 is a3.print (); // 2,3 42 is }
C ++ objects and classes function is passed a reference to a class object
Guess you like
Origin www.cnblogs.com/NiBosS/p/12152802.html
Recommended
Ranking