What's the difference between (int)50.f and int(50.f).
I know the first one is casting but what's the second one?
P.S
Both giving me 50.
Difference? (int)50.f and int(50.f) ?
-
killthesand
- Posts: 42
- Joined: Sat Sep 29, 2007 3:33 am
- Contact:
If I'm not mistaken, the first one is an explicit cast and the second one is directly calling the int constructor and returning an int. I think it's up to your compiler how the explicit cast actually works. Casting float to int shouldn't really matter. I found this example online which demonstrates a problem with explicit casts.
That code should compile without errors but it will crash when the pointer can't resolve the address of result()
If there was another constructor: CAddition(CDummy*); Then using the line padd = new CAddition(&d); should fix it. Calling a constructor should ensure a valid type.
Code: Select all
// class type-casting
#include <iostream>
using namespace std;
class CDummy {
float i,j;
};
class CAddition {
int x,y;
public:
CAddition (int a, int b) { x=a; y=b; }
int result() { return x+y;}
};
int main () {
CDummy d;
CAddition * padd;
padd = (CAddition*) &d;
cout << padd->result();
return 0;
}If there was another constructor: CAddition(CDummy*); Then using the line padd = new CAddition(&d); should fix it. Calling a constructor should ensure a valid type.
If you want safe casting, I'd rather use the C++ standard casts than the C standard.
float f = 10.0f;
int i = static_cast<int>(f);
const char* a = "blabla";
char* b = const_cast<char*>(a);
float f = 10.0f;
unsigned char* uc = reinterpret_cast<unsigned char*>(&f);
class Base {...};
class Derived : public Base {...};
Base* b = new Base();
Derived* d = dynamic_cast<Derived*>(b);
Using the C++ standard allows the compiler to do type-safety checks, it's clearer than C style casts as to what the programmer intends to do, and C style casts can't do dynamic casts either.
For a more in-depth, but quick, read:
http://www.informit.com/guides/content. ... seqNum=134
float f = 10.0f;
int i = static_cast<int>(f);
const char* a = "blabla";
char* b = const_cast<char*>(a);
float f = 10.0f;
unsigned char* uc = reinterpret_cast<unsigned char*>(&f);
class Base {...};
class Derived : public Base {...};
Base* b = new Base();
Derived* d = dynamic_cast<Derived*>(b);
Using the C++ standard allows the compiler to do type-safety checks, it's clearer than C style casts as to what the programmer intends to do, and C style casts can't do dynamic casts either.
For a more in-depth, but quick, read:
http://www.informit.com/guides/content. ... seqNum=134
Re: Difference? (int)50.f and int(50.f) ?
maybe there is a difference between memory handling, as far as int(50.f) seems to create a new integer while (int)50.f only casts it ???MasterGod wrote:What's the difference between (int)50.f and int(50.f).
I know the first one is casting but what's the second one?
P.S
Both giving me 50.
in either case for a single value it makes no difference for the result, but it does when you have a calculation !!!
Code: Select all
printf("%d\n", 50.5 + 50.5);
// output is 0, because we pass a floats result as integer (%d)
// so we have to cast the value passed to the string
// (yeah, I know it would be simpler to change to %f)
// now we can cast the values...
printf("%d\n", (int)50.5 + (int)50.5);
// but the output is 100 !!!
// or we can cast the result...
printf("%d\n", int(50.5 + 50.5));
// now the output is correct: 101
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:

http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Re: Difference? (int)50.f and int(50.f) ?
They are equivalent. The first is a standard C style cast, and the second one is called a function style cast. You would typically avoid a function style cast if the type you were casting to is made up of multiple tokens. For instance casting to unsigned long or something like that.MasterGod wrote:What's the difference between (int)50.f and int(50.f).
I know the first one is casting but what's the second one?
This isn't a problem with explicit casts. That is a problem with any cast, especially pointer casts. If you cast something that shouldn't be you'll get bad results.killthesand wrote:I found this example online which demonstrates a problem with explicit casts.
No. Both expressions result in a temporary int being created from the float.Acki wrote:maybe there is a difference between memory handling, as far as int(50.f) seems to create a new integer while (int)50.f only casts it ???
This has nothing to do with the type of cast you use. It has everything to do with order of operations and truncation of float.Acki wrote:in either case for a single value it makes no difference for the result, but it does when you have a calculation !!!
Code: Select all
// 50.5 + 50.5 = 101.0, then truncate to integer
const int a = int (50.5f + 50.5f);
// 50.5 truncated to integer becomes 50.
// 50 + 50 = 100
const int b = int (50.5f) + int (50.5f);
