基本上有予定义三个类:paralelogram
,point
和line
类,以及paralelogram
有一些vector
S的point
S和line
s,在所述paralelogram
I类正在定义compute_area
功能混乱上正在调用非const函数的const函数
double Paralelogram::compute_area() const
{
assert(!points.empty());
double h = points[3].distance_to_line(lines[0]); // point[3] is the last point in
// the vector however it is not a
// const object, and this gives a
// compile time error
double base = points[0].distance_to_point(points[1]);
return base*h;
}
编辑distance_to_line
功能是非const
double Point::distance_to_line(Line& l)
{
return l.distance_to_point(*this);
}
删除自定义函数的const
和但声明解决了这个问题,我的推理,而编码,被compute_area
不修改的对象,因此它可以const
,然而,这是只要正确的,因为它运行在const
对象和调用功能const
对象,对不对?
如果point
对象也不是const
也是,这是不再有效。并且因为它们不是const
,所以它是在const
删除后工作的原因。
这对我来说是一个令人迷惑的地方,我不修改对象,但是它使用的对象给出了问题,而且我在想我还没有改变这些对象,但显然我的混淆const
了解。还有一件事,这是否与this
Paralelogram
类的指针有关,如果是的话,你能澄清吗?
你的'distance_to_line'函数是一个非const函数,对吗? – Nawaz 2011-04-09 15:59:38
是的,我在帖子上做了一个修改。 – 2011-04-09 16:01:47
“平行四边形” – Johnsyweb 2011-04-09 16:05:03