casting operator - const vs non-const

前端 未结 3 745
野的像风
野的像风 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:42

    If you have a function like this:

    void f(const Number& n)
    {
      int n1 = n;
    }
    

    It will start giving compilation error if you remove const in the casting operator.

提交回复
热议问题