casting operator - const vs non-const

前端 未结 3 743
野的像风
野的像风 2021-01-11 10:43

I have this code sample:

class Number 
{ 
  int i;
  public:
    Number(int i1): i(i1) {}
    operator int() const {return i;}
};

What are

3条回答
  •  清酒与你
    2021-01-11 11:38

    The const version can be called regardless of whether the class Number instance is const or not. If the operator is declared non-const it can only be called on non-const entities - when you try to implicitly use it where it can't be called you'll get a compile error.

提交回复
热议问题