Meaning of 'const' last in a function declaration of a class?

后端 未结 10 2094
无人共我
无人共我 2020-11-21 06:37

What is the meaning of const in declarations like these? The const confuses me.

class foobar
{
  public:
     operator int () const         


        
10条回答
  •  广开言路
    2020-11-21 07:13

    The const means that the method promises not to alter any members of the class. You'd be able to execute the object's members that are so marked, even if the object itself were marked const:

    const foobar fb;
    fb.foo();
    

    would be legal.

    See How many and which are the uses of “const” in C++? for more information.

提交回复
热议问题