close
#include <string>
using namespace std;

class Ball {
public:
Ball();
Ball(double, const char*);
Ball(double, string&);

double radius() {
return _radius;
}

string& name() {
return _name;
}

void radius(double radius) {
_radius = radius;
}

void name(const char *name) {
_name = name;
}

void name(string& name) {
_name = name;
}

double volumn() {
return (4 / 3 * 3.14159 * _radius * _radius * _radius);
}

private:
double _radius; // 半徑
string _name; // 名稱
};
 
When we create two objects likes b1(1, "RBall")與b2(2, "GBall") , 
although their data member are individual , but the member function are the same.
so , how does the member function know which function call him?
The answer is this pointer , this pointer can help us deal with this problem.
when you call name() method , the content actually is " return this->_name " ,
this pointer stores address of b1 if the object b1 call the method , 
it is the same situation , if the object b2 call the method , this pointer will store address of object b2.
 

 

ref : http://caterpillar.onlyfun.net/Gossip/CppGossip/thisPointer.html

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JerryCheng 的頭像
    JerryCheng

    KwCheng's blog

    JerryCheng 發表在 痞客邦 留言(0) 人氣()