[英]Would this be considered an Accessor? (C++)
抱歉,格式糟糕,但我只是想確定一下以下內容是否被視為訪問器。
所以我的班級定義看起來像這樣...
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
梯子會使代碼難看又笨拙))。
一些術語
通過這個不,不是真的。 訪問器是從訪問派生的 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.