Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > implicit conversion

Reply
Thread Tools

implicit conversion

 
 
Gary Wessle
Guest
Posts: n/a
 
      01-28-2007
Hi

I am getting the warning below.

************************************************** **************
cd ~/toy/
make -k
g++ -gdwarf-2 -c -o my.o my.cpp
my.cpp: In function ‘int main()’:
my.cpp:7: warning: converting to ‘int’ from ‘double’
g++ -Wall -gdwarf-2 -o proj my.o -lboost_filesystem -lboost_thread -L.

Compilation finished at Mon Jan 29 08:03:34
************************************************** **************

the code is

************************************************** **************
#include <iostream>
using namespace std;

int main(){
double b = 0.04;
int a;
a *= b;
cout << a << endl;
}
************************************************** **************

do I leave the compiler to do the conversion or should I
static_cast<double> (a) before a *= b?
then
static_cast<int> (a) again when finished?

too much work...

thanks

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-28-2007
Gary Wessle wrote:
> Hi
>
> I am getting the warning below.
>
> ************************************************** **************
> cd ~/toy/
> make -k
> g++ -gdwarf-2 -c -o my.o my.cpp
> my.cpp: In function ‘int main()’:
> my.cpp:7: warning: converting to ‘int’ from ‘double’
> g++ -Wall -gdwarf-2 -o proj my.o -lboost_filesystem -lboost_thread -L.
> ************************************************** **************
> #include <iostream>
> using namespace std;
>
> int main(){
> double b = 0.04;
> int a;
> a *= b;
> cout << a << endl;
> }
> ************************************************** **************
>
> do I leave the compiler to do the conversion or should I
> static_cast<double> (a) before a *= b?
> then
> static_cast<int> (a) again when finished?
>

The result of the expression a*b will be double, which is implicitly
converted to int on assignment.

Casting won't change that, it'll just hide the compiler warning.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Ivan Novick
Guest
Posts: n/a
 
      01-29-2007
On Jan 28, 1:08 pm, Gary Wessle <phd...@yahoo.com> wrote:

> #include <iostream>
> using namespace std;
>
> int main(){
> double b = 0.04;
> int a;
> a *= b;
> cout << a << endl;}******************************************** ********************
>
> do I leave the compiler to do the conversion or should I
> static_cast<double> (a) before a *= b?
> then
> static_cast<int> (a) again when finished?


What are you trying to do? a is not initalized, so your program
produces undefined results. If you give us some reasonable code to
look at, we can probably help.
----
Ivan
http://www.0x4849.net


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with implicit conversion Boris C++ 3 08-26-2004 02:03 PM
Implicit/explicit conversion al C++ 2 01-07-2004 10:18 AM
Implicit conversion is evil? Russell Reagan C++ 2 10-27-2003 04:44 PM
Question on additional decimals in implicit conversion Jacob Java 7 10-03-2003 10:23 PM
implicit vs. explicit type conversion for string vs. (char *) dmoos AT esigma-systems DOT de C++ 2 06-26-2003 04:32 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57