473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Difference between int() and (int)

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::operator Record*(const State& st);

James
Jul 22 '05 #1
5 1277
James Aguilar wrote:
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.

Jul 22 '05 #2
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
Jul 22 '05 #3
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.
Jul 22 '05 #4
Ron Natalie wrote:
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.


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
Jul 22 '05 #5
Victor Bazarov wrote:
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).
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: David | last post by:
what's the differences between: int main(int argc,char* argv){ ... } and: int main(int argc,char** argv){ ...
3
by: Materialised | last post by:
Hi, I often see 2 different declairations of the main() function, and I am unsure of the difference. They are int main(int argc, char *argv) int main(int argc, char **argv) What is the...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
39
by: August Karlstrom | last post by:
Hello all, If c is a char then is there any difference between c = '\0' and c = 0
21
by: Rich | last post by:
I was considering C# for developing a scientific application, but I have noticed a ~30% difference between VC++ .NET and C# on the same machine, under identical conditions: double a = 0,b = 0, c...
5
by: chenedor | last post by:
Hi all I am a bit confuse about unboxing... what is the difference and what is best practice ? object o = __box(1234); int n = *dynamic_cast<__box int*>(o); of
8
by: arun | last post by:
Hi all,, I want to know if there is any performance difference between the following sets of declarations. int i, j; and int i;
13
by: In a little while | last post by:
thanks
6
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first...
11
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.