Home > Back-end >  Based problem [C] how to judge whether the object is const inside a class
Based problem [C] how to judge whether the object is const inside a class

Time:03-23

This code in the c + + primer is a little not understand
 
The class Screen
{
Public:
/* typedef STD: : string: : size_type pos.
Screen & amp; The set (char);
Screen & amp; Set (pos, pos, char);
Screen ()=default;
Screen (pos ht, pos wd, char c) : height (ht), width (wd), contents (ht * wd, c) {} */
Screen & amp; The display (STD: : ostream & amp; OS)
{
Do_display (OS);
Return * this;
}
Const Screen & amp; The display (STD: : ostream & amp; OS) const
{
Do_display (OS);
Return * this;
}
/* char the get () const
{
Return contents (cursor);
}
Char get (pos ht, pos wd) const;
Screen & amp; Move (pos r, pos c); */
Private:
/* pos cursor=0;
Pos height=0, width=0; */
STD: : string contents;
Void do_display (STD: : ostream & amp; OS) const {OS & lt; };
//...
//the main () {
Const Screen blank (5, 3);
Blank. The display (cout);
//}

Class is how to determine blank is const, then let him call const version of the display? Personal feeling is because the implicit parameter blank this type is const Screen * const, so at the time of overloaded function called discovery and const version of the display to match better, so call const version of the display? Understand it right?

CodePudding user response:

Compile time will know
  • Related