我有一个函数,它有两个类实例作为参数:
void cookPasta(const Tomato& tomato, const Meat* meat)
{
if (meat != nullptr)
cookPastaWithMeat(tomato, *meat);
else
cookPastaWithoutMeat(tomato);
}
如该函数所示,一个实例Tomato
总是需要的,而Meat
是可选的并且nullptr
可以改为通过。我这样做是为了允许cookPasta
即使用户从未声明过该函数的实例,也会调用该函数Meat
class.
在函数签名中混合引用和指针是不好的做法吗?
使用这种方法你失去的一件事是可能会临时通过Meat
,因为无法获取其地址。
为什么不通过简单地重命名来使用重载cookPastaWithMeat
and cookPastaWithoutMeat
?
void cookPasta(const Tomato& tomato, const Meat& meat);
void cookPasta(const Tomato& tomato);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)