2016-09-22 38 views
0

我是C++新手。你能告诉我为什么std :: vector :: front有两个定义,它们有什么不同,以及它们是如何被调用的?为什么std :: vector :: front有两个定义? (C++)

功能似乎有两种定义,

  • 参考前面();
  • const_reference front()const;

当我在Web上查找函数时,我注意到了这两个定义。以下两个网站似乎说了相同的两个定义。

http://www.cplusplus.com/reference/vector/vector/front/

http://en.cppreference.com/w/cpp/container/vector/front

+0

这是为了保持常量的正确性。一个用于非常量,一个用于常量。 –

+1

@GillBates:你的名字让我笑了起来。 – 2016-09-22 14:24:49

回答

1

const版本用于通过constthis指针由于重载解析。如果一个非const对象被返回,那么你就可以破解const

const版本由非constthis指针使用。你找回一个你可以修改的对象。

我们把这称为const正确性

+0

谢谢@FaceyMcFaceFace。 – Toshihiro

相关问题