抱歉,格式糟糕,但我只是想确定一下以下内容是否被视为访问器。
所以我的班级定义看起来像这样...
class work {
public:
void getInput(); //Mutator
void output(); //Accessor?
//...
所以这是功能。
void work::output()
{
dayofweek = day + getMonthvalue(month, year) + ....;
final = dayofweek % 7;
if(final == 0)
cout << "The day of the date is Sunday\n";
else if(final == 1)
cout << "The day of the date is Monday\n";
else if(final == 2)
cout << "The day of the date is Tuesday\n";
/*.
.
.*/
else {
cout << "The day of the day is Saturday\n";
}
}
最佳答案
显示为output
的内容通常会写为inserter
:
class work {
// ...
friend std::ostream &operator<<(std::ostream &os, work const &w) {
static char const *names[] = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
return os << names[getDayOfWeek(w)];
}
};
只是要清楚一点:
inserter
是因为它在流中插入了某种类型的项而得名。镜像(从流中获取某种类型的项)是extractor
。如果您真的坚持使用正确的名称来表示现在的代码,我的立场是使用
mistake
(将输出强制为cout
会失去灵活性,使用if
阶梯会使代码难看,笨拙)。关于c++ - 是否将其视为访问器? (C++),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27414498/