C + + Object-oriented class member function this pointer

Source: Internet
Author: User

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

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.