繁体   English   中英

如何使用调用函数的对象的参数?

[英]How do I use the parameters of the object that I am calling a function on?

//Function to find the distance between 2 points
TLength CPoint::Distance(const CPoint& point) const
{
    TLength horiz = abs(point.X() - OBJECT.X());
  TLength vert = abs(point.Y() - OBJECT.Y());
  TLength dist = sqrt(pow(horiz,2) + pow(vert,2)); 
  return dist;
};


int main()
{
const CPoint a = CPoint(4,5);
const CPoint b = CPoint(1,1);

a.Distance(b);

};

是否有一个术语可以代替 OBJECT 来使用函数 Distance 中的 a 值?

this是 self 对象上的指针,所以

TLength CPoint::Distance(const CPoint& point) const
{
    TLength horiz = abs(point.X() - this->X());
    TLength vert = abs(point.Y() - this->Y());
    TLength dist = sqrt(pow(horiz, 2) + pow(vert,2)); 
    return dist;
}

它甚至是“隐式的”(除非需要(从属名称,与成员同名的本地名称,...))

TLength CPoint::Distance(const CPoint& point) const
{
    TLength horiz = abs(point.X() - X());
    TLength vert = abs(point.Y() - Y());
    TLength dist = sqrt(pow(horiz, 2) + pow(vert,2)); 
    return dist;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM