对于任何一个class,都有一个this pointer可以指代它,它可以指向这个class中的任何member functions,注意不包括friend functions
#include <iostream>
using namespace std;
class Box
{
double width;
public:
Box(double wid):width(wid)
{
cout << "object is created" << endl;
}
double getwidth()
{
return width;
}
int comparef(Box box)
{
return this->getwidth() > box.getwidth(); // 也可以box.width(不管width作为private还是public,当box作为参数时,都可以用点)
}
};
int main()
{
Box box1(12),box2(13);
if(box1.comparef(box2)) //this is important
cout << "box1 is bigger than box2";
else
cout << "box1 is not bigger than box2";
return 0;
getvalue(box1);
return 0;
}