Jump to content
  • Advertisement
Sign in to follow this  
johnnyBravo

In c++, is '(int)5.0f1' the same as 'int(5.01f)' ??

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

Advertisement
Whats the difference if you have a class?

is (Type) 123 the same as operator Type() (123)
and Type(123) the same as Type( 123 ) ?

Yes I know, braindamaged... just wondering...
Thermo

Share this post


Link to post
Share on other sites
Quote:
Original post by Konfusius
Whats the difference if you have a class?

is (Type) 123 the same as operator Type() (123)
and Type(123) the same as Type( 123 ) ?

Yes I know, braindamaged... just wondering...
Thermo


TYPE(/*add 2^1024 whitespace here */value) is the same as TYPE(value) (well, provinding you have enough memory and disk space to parse or store the source file :D): it calls the TYPE object constructor (this is not a cast).

The answer I gave to the OP only deals with scalar types.

(TYPE)x and x.operator TYPE() should be the same (I don't see where the problem should be). operator Type() (x) is just plain weird from a design point of view (well, you are exposing the content of object x to an external function ?), but if this is the correct writing then I don't see why it should not be the same.

Regards,

Share this post


Link to post
Share on other sites
TypeA()(x) would construct an object of type TypeA usign the default constructor, then call its operator () function with the value of x.

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
Yes, it is. I tend to use int(x) because I find it clearer than (int)x.

Regards,


I know it's practically the same, but is it also conceptually so?? I thought this: int(x) invokes the constructor for type int, whereas this: (int)x casts x to an int? I mean the end result is still the same I guess but ... oh I really don't know what I'm talking about lol.
At any rate, in C++ it's better to use the new cast operators anyway (static_cast, const_cast, reinterpret_cast, dynamic_cast).

Share this post


Link to post
Share on other sites
Quote:
Original post by Extrarius
TypeA()(x) would construct an object of type TypeA usign the default constructor, then call its operator () function with the value of x.


Even if you specify the operator keyword before ? I must admit that this is the kind of code construct that I never use (and I'll probably never use them - sounds so weird that I can't see wether they are a good thing or not).

Regards,

Share this post


Link to post
Share on other sites
Quote:
Original post by Emmanuel Deloget
Quote:
Original post by Konfusius
operator Type() (123)


operator Type() (x) is just plain weird from a design point of view...


Seriously, I wasn't thinking there. of course, i meant x.operator Type(), not the cr*p i typed there.

Thanks for clearing it up.

Thermo

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!