据说以下内容比将 First() 和 Second() 作为公共成员更好。我相信这几乎同样糟糕。
// Example 17-3(b): Proper encapsulation, initially with inline accessors. Later
// in life, these might grow into nontrivial functions if needed; if not, then not.
template<class T, class U>
class Couple {
public:
Couple() : deleted_(false) { }
void MarkDeleted() { deleted_ = true; }
bool IsDeleted() { return deleted_; }
private:
T first_;
U second_;
bool deleted_;
T& First() { return first_; }
U& Second() { return second_; }
};
如果您要提供一种访问类外部私有变量的方法,那有什么意义呢?功能不应该是
T First(); void(or T) First(const T&)