※ 引述《wowslr (柠檬子)》之铭言:
推 kwpn:因为const IDrive& getDrive()回传const reference 05/19 12:39
→ kwpn:但foo(IDrive &drive)想要的是non-const 05/19 12:41
→ wowslr:所以这种情况我应该要提供 non-const 和 const 两个版本? 05/19 14:31
→ CaptainH:或许 foo 的引数可以设计成 const IDrive & 05/19 15:31
→ CaptainH:或是IDrive&& 05/19 15:32
这看你究竟想做什么而定
传入参考作为引数隐含着函数本身会改变该引数内部值的概念
如果 foo 不会修改传进去的 drive, 那么就可以让它直接接受 const IDrive &
这样在函数语义上也会比较清晰
反之如果 foo 会修改 drive, 那么当然你就必须为它提供 non-const 版本的 getter