What is an “operator int” function?

前端 未结 5 1083
南旧
南旧 2020-12-08 02:10

What is the \"operator int\" function below? What does it do?

class INT
{
   int a;

public:
   INT(int ix = 0)
   {
      a = ix;
   }

   /* Starting here:         


        
5条回答
  •  无人及你
    2020-12-08 02:59

    operator int() is a conversion operator, which allows this class to be used in place of an int. If an object of this type is used in a place where an int (or other numerical type) is expected, then this code will be used to get a value of the correct type.

    For example:

    int i(1);
    INT I(2); // Initialised with constructor; I.a == 2
    i = I;    // I is converted to an int using `operator int()`, returning 2.
    

提交回复
热议问题