Difference between int() and (int)

J

James Aguilar

I have searched about this issue, but I can't find anything because Google's
search feature ignores parentheses.

Here's the issue. When I write a conversion operator such as

//Assume Record is some UDT
operator int() (const Record& rec);

it is (unless I am mistaken) called when I write something like this:

int main()
{
Record rec = Record(5);
int numRec = int(rec);
cout << numRec << endl;
}

So what gets called when I do something like this:

int numRec = (int) rec;

?

Also, as a side note, can I add conversion operators for enums?

enum State { BLAH = 0, MOREBLAH = 1 };

operator Record*(const State& st);
//or is it
State::eek:perator Record*(const State& st);

James
 
R

Ron Natalie

James said:
int numRec = int(rec);
int numRec = (int) rec;

Both conversion forms perform the same conversion operation in this
case. Note that you can only use the first form with simple (single
identifier types). That is:
unsigned int numRec = unsigned int(rec);
is ill-formed.
Also, as a side note, can I add conversion operators for enums?

No.
 
J

JKop

unsigned int numRec = unsigned int(rec);
is ill-formed.


....?

So is:


unsigned numRec = unsigned(rec);


ill-formed?

And how would one create a nameless temporary of "unsigned long", seeing as:

unsigned long()


is ill-formed?!


-JKop
 
R

Ron Natalie

JKop wrote:

So is:
unsigned numRec = unsigned(rec);

ill-formed?

Good question. Now you've sent me looking towards the standard to see
if simple-type-specifier can have unsigned. And yes, it does.
simple-type-specifier is defined as any (optionally qualified)
identifier typename, or one of the following: char, wchar_t, bool,
short, int long, singed, unsigned, float, double or void.

And how would one create a nameless temporary of "unsigned long", seeing as:

unsigned long()
Well,
const unsigned& numRec = (unsigned long) 4;
would still work.

A cast and a single-arg'd functional conversion are the same semanticly.
 
V

Victor Bazarov

Ron said:
JKop wrote:




Good question. Now you've sent me looking towards the standard to see
if simple-type-specifier can have unsigned. And yes, it does.
simple-type-specifier is defined as any (optionally qualified)
identifier typename, or one of the following: char, wchar_t, bool,
short, int long, singed, unsigned, float, double or void.

Actually there is no 'int long'. There is 'long int', however. But
I think you just lost a comma in your list, between the "int" and the
"long".

Trying to avoid misinterpretation of your words, that's all...

V
 
R

Ron Natalie

Victor said:
Actually there is no 'int long'. There is 'long int', however. But
I think you just lost a comma in your list, between the "int" and the
"long".
Yes, there should be a comma there (and for those new English types, one
after "double" as well).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top