C++ primer中這段代碼有一點不明白
class Screen
{
public:
/*typedef std::string::size_type pos;
Screen &set(char);
Screen &set(pos, pos, char);
Screen() = default;
Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {}*/
Screen &display(std::ostream &os)
{
do_display(os);
return *this;
}
const Screen &display(std::ostream &os) const
{
do_display(os);
return *this;
}
/*char get() const
{
return contents[cursor];
}
char get(pos ht, pos wd) const;
Screen &move(pos r, pos c);*/
private:
/*pos cursor = 0;
pos height = 0, width = 0;*/
std::string contents;
void do_display(std::ostream &os) const { os << contents; }
};
//...
//main(){
const Screen blank(5, 3);
blank.display(cout);
//}
類是如何判斷blank是const,然后讓他呼叫const版的display的?個人感覺是因為blank的隱式形參this的型別是const Screen *const,所以在呼叫多載函式的時候發現和const版的display匹配的更好,所以呼叫const版display?這樣理解對嗎?
uj5u.com熱心網友回復:
編譯期間就會知道啊轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/269212.html
標籤:C++ 語言
