Solved Is there a "new" style for casting?
-
@aha_1980
thanks; I Didn't make that clear enough, my bad for simply copy and pasting :-) -
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
Don't post screenshots of your code, makes it difficult to actuallly quote it and modify it. -
@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.
-
@jsulm is it look like this
int x = static_cast<int>(screensize().width() * .8);
int y = static_cast<int>(screensize().height() * .7);If not tell me the difference between new cast and old...plz
-
@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 -
-
@jsulm ok now understand the difference.
Thanks... -
@J.Hilk Both are C-style casts
-
-
@saber you wrote the clever statement ever.
!!!! -
@jsulm thats not what @aha_1980 said earlier, and what I read from my trusted source
(http://www.cplusplus.com/doc/tutorial/typecasting/) :-)the new (qtcreator)clang code model seems to agree as well, as int(xy) is not flaged as old style casting.
-
@J.Hilk Yeah, I think I was wrong on function style cast you mentioned
-
i got more warnings to fix.
switch( m_model->state() ) { case Battery::FullyCharged: ui->statusEdit->setText( tr( "Full Charged" ) ); ui->timerLblEdit->setText( tr( "Full Charged" ) ); break; case Battery::Discharging: addSeconds = static_cast<int>(m_model->toEmpty()); addSeconds = rate != 0 && addSeconds == 0 ? ( energy - energyEmpty ) * 4 / rate: addSeconds; ui->statusEdit->setText( tr( "Discharging" ) ); ui->timerLblEdit->setText( tr( "Discharged in : " ) ); break; case Battery::Charging: addSeconds = static_cast<int>(m_model->toFull()); addSeconds = rate != 0 && addSeconds == 0 ? ( energyFull - energy ) * 4 / rate : addSeconds; ui->statusEdit->setText( tr( "Charging" ) ); ui->timerLblEdit->setText( tr( "Charged in : " ) ); break; default: ui->statusEdit->setText( tr( "No Battery" ) ); ui->timerLblEdit->setText( tr( "No Battery" ) ); break; }