gpt4 book ai didi

c++ - 为什么 "C++ Core Guidelines"推荐首选独立函数而不是类成员?

转载 作者:行者123 更新时间:2023-11-28 01:48:26 25 4
gpt4 key购买 nike

<分区>

C++ Core Guidelines section C.4建议“仅当需要直接访问类的表示时才使函数成为成员”,使用以下示例:

class Date {
// ... relatively small interface ...
};

// helper functions:
Date next_weekday(Date);
bool operator==(Date, Date);

我不明白这里的推理。在我看来,这将是一个更好的选择:

class Date {
// ... relatively small interface ...

Date next_weekday(Date const&) const;
bool operator==(Date const&) const;
};

这个推理有什么好处?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com