【发布时间】:2010-10-14 22:26:18
【问题描述】:
在我维护的一个项目中,我看到很多这样的代码,用于简单的get/set 方法
const int & MyClass::getFoo() { return m_foo; }
void MyClass::setFoo(const int & foo) { m_foo = foo; }
这样做的意义何在?
int MyClass::getFoo() { return m_foo; } // Removed 'const' and '&'
void MyClass::setFoo(const int foo) { m_foo = foo; } // Removed '&'
传递对原始类型的引用应该需要与传递类型的值本身相同(或更多)的工作,对吧?
毕竟只是一个数字...
这只是一些尝试的微优化还是有真正的好处?
【问题讨论】:
标签: c++ reference constants primitive