你觉得要严格使用const
每个值将不会被改变的时间或通过参数为指针,只有当数据将被修改,这一点很重要?正确的参数为const或操作数
我想正确地做事,但如果作为参数传递的struct
尺寸很大,难道您不想传递地址而不是复制数据吗?通常它好像刚刚宣布struct
参数作为一个操作数是最具功能性。
//line1 and line2 unchanged in intersect function
v3f intersect(const line_struct line1, const line_struct line2);
//"right" method? Would prefer this:
v3f intersect2(line_struct &line1, line_struct &line2);
两个第一原型是不等价的。在第二个函数中,您可以修改函数体中的参数。 – ouah 2012-02-23 21:40:26
根据编译器的不同,将const应用于按值传递参数可能有助于优化。编译器应该将const限定符解释为不允许写入参数的语句。 – Throwback1986 2012-02-23 21:45:22
@ouah:你说得对,补充说他们在外部行为上是相同的。 – 2012-02-23 21:47:27