• Advertisement

Archived

This topic is now archived and is closed to further replies.

Is (int (Answer)) a valid c++ statement?

This topic is 6508 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I finaly registered my nick after reading the message board for a year Anyway, to the real question. I was reading the tutorial on eastcoastgames.com and I was entering in one of their programs (chapter 3, roulette game), and came across the line that says "if ((int (Answer)) == Wheel)" I was wondering if this is proper c++ syntax because it gives me an error when I try to compile it using VC++ 6.0. Any ideas or is there a way to correct it? How about if use "if(Answer == Wheel)" ? thanks, Hitchhiker wrong is wright

Share this post


Link to post
Share on other sites
Advertisement
Actually, I don''t think it is. Try changing it to

if (((int)Answer) == Wheel)
...

if (Answer == Wheel) will probably work, but the compiler might give you a warning about the type cast it has to do.

Share this post


Link to post
Share on other sites
That should be valid C++ syntax. The stripped down casting conventions for built in-types are now written as: ''typename(value)'' like ''int(Answer)''. This is designed so it mirrors the usage of copy constructors. And the extra set of parentheses around the whole thing shouldn''t do anything at all. If VC6 doesn''t like it, there''s something else wrong... I NEVER use the old casting syntax anymore: ''(typename) value'' because frankly, it just doesn''t make as much sense when you read it.

-Brian

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
were you doing this in a .c file (BAD), or a .cpp file?

Share this post


Link to post
Share on other sites
Actually, I think it WOULD be a valid statement. There are two ways to type cast. One, which is the most popular way of doing it, is:

if ( (int)(Answer) == Wheel );

The other, uses a semi-function (macro, perhaps?) int(). Like this:

if ( int(Answer) == Wheel );

So, it SHOULD be valid, but I don''t know what the error is for. Good luck.


ColdfireV

Share this post


Link to post
Share on other sites
Personaly, I've always done it the quick way,
if(var_name == other_var_name_or_value), and I never had any problems, but in a .C file you would get errors I think.

But I may be wrong since its been like 4 years since I last used pure C!

Edited by - Blah! on 4/19/00 9:39:43 PM

Share this post


Link to post
Share on other sites
I want to thank everyone who replied. I did some reading and the proper command was if((int)Answer == Wheel)
thanks a bunch.


wrong is wright

Share this post


Link to post
Share on other sites
Can''t you see the problem guys?

look:
((int (Answer)) == Wheel)

It''s a parentheses problem... sorta. They''re using some kind of weird (and apparently non-valid) casting syntax.

Yes, it should be like if((int)Answer==Wheel) or similar thing. The way it was had the "int" parentheses encompassing "Answer" as well as "int". To me that looks like it''s saying "cast a datatype to an undeclared data type (or vica versa)" which... doesn''t work, to say the least.

I know the problem was already solved but I felt like contributing.

-shpook

Share this post


Link to post
Share on other sites
All new C++ books (for example: Stroustrup 3 rev.) recommend using the "new" c++ casts. In this case this would be:

if ((dynamic_cast(Answer) == Wheel)

Sometimes compilers are generating slow code for the "new" casts.

Bjoern

Share this post


Link to post
Share on other sites
God I hate it! The message board has eaten my code...

after dynamic_cast there should be a "<" followed by a "int" followed by a ">" and then (Answer). I hope that these symbols will appear....

Bjoern

Share this post


Link to post
Share on other sites
Although it''s preferable to use the new C++-style casts in C++, for this example you certainly don''t want to use dynamic_cast.

dynamic_cast is meant for casting pointers to base classes to pointers to derived classes, and it uses RTTI to check the correctness of the cast. In case of an incorrect cast, an exception is thrown.

Here, you''d use static_cast(Answer), instead of the old C-style (int)Answer cast (which is still valid in C++, as others have pointed out), if you were to use the new C++-style casts.

Erik

Share this post


Link to post
Share on other sites
Ups, yeah - Erik is right. static_cast is the magic word to cast primitive/non class-inherited types.

Sorry for the wrong info!

Bjoern

Share this post


Link to post
Share on other sites
I just had to say something :

I read in a book that int(Answer) and (int)Answer end-up with the same result, but the int(Answer) syntax tells the compiler to do some extra error checking, so this may be where the error is coming from: extra error checking.

Share this post


Link to post
Share on other sites
if (static_cast< int >(Answer) == Wheel)

&lt; for <, and &amp; for &

The reason it deletes these characters is because the forum accepts HTML.

Edited by - CobraA1 on 4/27/00 6:57:52 PM

Share this post


Link to post
Share on other sites

  • Advertisement