46

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

class INT { int a; public: INT(int ix = 0) { a = ix; } /* Starting here: */ operator int() { return a; } /* End */ INT operator ++(int) { return a++; } }; 
5
  • What bold code ? Otherwise it's basically a wrapper for int with conversion to int and operator ++. Commented Sep 28, 2010 at 16:22
  • 3
    Looking at the source of the original post, operator int() is supposed to be the bold code. Commented Sep 28, 2010 at 16:29
  • 8
    If I could, I would honestly vote to re-open this. The original question was formatted poorly but had a segment of code clearly in bold. A later edit (not by the OP) was what made the code un-bold and hence made the question seem incoherent. Commented Sep 28, 2010 at 16:31
  • 2
    @All : I am the culprit here. Voted to reopen. Commented Sep 28, 2010 at 16:34
  • It solves the halting problem ;) Commented Sep 28, 2010 at 16:35

5 Answers 5

46

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 
Sign up to request clarification or add additional context in comments.

3 Comments

Broken cast operator link; can you try to find the new one please?
Interesting sidenote: conversion operators are the only functions that will resolve based on the return type. This allows all sorts of cool tricks that most people believe are impossible.
Also, conversion operators can cause weird quirks occasionally. Prefer to mark them explicit whenever possible/reasonable.
18

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. 

2 Comments

I can't find any documentation for this on cppreference.com. Can you?
@GabrielStaples I know it's an old comment, but if you are still interested I've added a link to cppreference. The page's title is "User-defined conversion function", but the URL calls it a "cast operator".
7

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 

Comments

3

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?

Comments

2

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++; }; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.