Is there a "new" style for casting?
-
@saber
take a look at the c++ basic'shttp://www.cplusplus.com/doc/tutorial/typecasting/
the tl;dr version
double x = 10.3;
int y;
y = int (x); // functional notation
y = (int) x; // c-like cast notationc++ casting
dynamic_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
static_cast <new_type> (expression)
const_cast <new_type> (expression) -
@J.Hilk said in Is there a "new" style for casting?:
y = int (x); // functional notation
Note that this is also a valid C++ style cast, which is still used for numeric types, as it's less noise than the xxx_cast<>() versions.
-
can you modify those warnings lines in "new cast"
it will make it clear for me.
thanks.
@jsulm can you ? please// Check available space on destination before we start struct statfs info; statfs(newPath.toLocal8Bit(), &info); if ((qint64) info.f_bavail * info.f_bsize < total) { // here // If it is a cut/move on the same device it doesn't matter if (cutList.count()) { qint64 driveSize = (qint64) info.f_bavail*info.f_bsize; // here statfs(files.at(0).path().toLocal8Bit(),&info); // Same device? if ((qint64) info.f_bavail*info.f_bsize != driveSize) { // here emit copyProgressFinished(2, newFiles); return 0; } } else { emit copyProgressFinished(2, newFiles); return 0; } }
-
@saber said in Is there a "new" style for casting?:
can you ? please
I already did in one of your other threads
-
@J.Hilk i did others
if (cutList.count()) { qint64 driveSize = static_cast<qint64>(info.f_bavail) * (info.f_bsize) ; statfs(files.at(0).path().toLocal8Bit(),&info); // Same device? if (static_cast<qint64>(info.f_bavail) * (info.f_bsize) != driveSize) { emit copyProgressFinished(2, newFiles); return 0; }
-
@J.Hilk i got other warnings like
int x = screensize().width() * .8; int y = screensize().height() * .7;
and warning is
/home/shaber/Desktop/Folder/work3/coreApps/app/corefm/corefm.cpp:29: warning: implicit conversion turns floating-point number into integer: 'double' to 'int'
-
@saber You are multiplying an int with a double and assign the result to an int, this is dangerous.
To get rid of the warning you can cast the result of the multiplication to int or (even better) think about what you're doing and whether rounding in a defined way would be better. -
@saber said in Is there a "new" style for casting?:
@J.Hilk i got other warnings like
int x = screensize().width() * .8; int y = screensize().height() * .7;
and warning is
/home/shaber/Desktop/Folder/work3/coreApps/app/corefm/corefm.cpp:29: warning: implicit conversion turns floating-point number into integer: 'double' to 'int'
of course,
this breaks down toint = int * double;
int = double <- precision loss, truncated after the decimal pointthis is an implicit conversion of double to int, therefore cast it alsoif thats intendet behaviour:
int x = static_cast<int>(screensize().width() * .8);
or round it
int x = qRound(screensize().width() * .8);
-
@Abrar said in Is there a "new" style for casting?:
int x = int(screensize().width() * .8);
int y = int(screensize().height() * .7);This would bring back the warning about old style cast.
-
@Abrar said in Is there a "new" style for casting?:
static_cast<int>(screensize().width() * .8)
Yes, this is C++ type cast.
(int)(someVariabl);
Old C style cast which should not be used in C++.
See https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used