开发者

What is an "operator int" function?

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

class INT
{
   int a;

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

   /* Starting 开发者_StackOverflow中文版here: */
   operator int()
   {
      return a;
   }
   /* End */

   INT operator ++(int)
   {
      return a++;
   }
};


The bolded code is a conversion operator. (AKA cast operator)

It gives you a way to convert from your custom INT type to another type (in this case, int) without having to call a special conversion function explicitly.

For example, with the convert operator, this code will compile:

INT i(1234);
int i_2 = i; // this will implicitly call INT::operator int()

Without the convert operator, the above code won't compile, and you would have to do something else to go from an INT to an int, such as:

INT i(1234);
int i_2 = i.a;  // this wont compile because a is private


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.


First things first:

$12.3.1/1 - "A constructor declared without the function-specifier explicit specifies a conversion from the types of its parameters to the type of its class. Such a constructor is called a converting constructor."

In your example, INT is a User Defined class that has a converting constructor from 'int'.

Therefore the following code is well-formed:

INT i(1024);     // direct initialization syntax

This means that you can get an INT object from an integer. However what does one do, if the INT object has to be converted back to an integer? Transitivity?

One can say that the class INT can provide a member function to return the encapsulated integer member

int x = i.geta();

This however is not very intuitive and is not a standardized approach. Also it is not intuitive when it comes to how built-in types work in such situations.

int z = 0;
int y1 = z; // copy initialization or
int y2(z);  // direct initialization
double d = (int )z; // explicit cast

Therefor the Standard allows for such standardization and intuitiveness of converting User Defined Types by saying:

$12.3/2 - "A member function of a class X having no parameters with a name of the form [...]

operator conversion-type-id

[...]specifies a conversion from X to the type specified by the conversion-type-id. Such functions are called conversion functions. No return type can be specified. If a conversion function is a member function, the type of the conversion function (8.3.5) is “function taking no parameter returning conversion-type-id”.

This makes all of the following well-formed and retains harmony with the way built-in types work is

int y1 = i; // copy initialization or
int y2(i);  // direct initialization
double d = (int )i; // explicit cast


It looks like it make an INT class which behaves a little like the regular int, just that some other operators are not yet defined.

Is this a homework problem?


Seems like it's a question from a classroom, so I'll invite you to check the documentation on how to create a class.

class Foo
{
public
    Foo() {} // Constructor

    Foo operator++ {} // Operation ++ on foo like:
    // Foo foo;
    // foo++;
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜