Each class member function involves only one object, that is, the object that invokes it. However, sometimes a method might involve two objects, in which case you need to use the this pointer of C + +
Assuming that the method is named Topval (), the function call Stock1.topval () accesses the Stock1 object data; Stock2.topval () accesses the Stock2 object data;
If you want to compare the method's two objects, you must pass the second object as a parameter to it. This time involves implicit and explicit:
top = Stock1.topval (Stock2);
Implicit access to the STOCK1, shows the access to the Stock2, this represents the Stock1, *this is the object pointer
//easy to use C + +//5. Object-oriented this pointer, object-oriented object pointer completion#include <iostream>#include<string>using namespacestd;classOver {Private: Public: inth =2;};classStock {Private: /*Data*/ inth; Public: Stock (intABC); ~Stock (); intABC (); Stock&topval (Stock &s); Stock&topval1 (Over &s);}; Stock::stock (intABC) {h=ABC; printf ("%d\n", h);} Stock::~Stock () {}intstock::abc () { This->h =Ten; printf ("%d\n", This-h); printf ("%d\n", h); return 0;} Stock&stock::topval (Stock &s) {printf ("this-h:%d, s2.h:%d\n", This-h, s.h);} Stock&stock::topval1 (Over &s) {printf ("this-h:%d, s2.h:%d\n", This-h, s.h);}intMainintargcChar Const*argv[]) {Stock S1 (123); S1.ABC (); Stock S2 (222); S1.topval (S2); Over S3; S1.topval1 (S3); return 0;}
C + + Object-oriented class member function this pointer