Jump to content
  • Advertisement

Archived

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

Hitchhiker

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

This topic is 6603 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!