implicit conversion

Discussion in 'C++' started by Gary Wessle, Jan 28, 2007.

  1. Gary Wessle

    Gary Wessle Guest

    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
    Gary Wessle, Jan 28, 2007
    #1
    1. Advertising

  2. Gary Wessle

    Ian Collins Guest

    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.
    Ian Collins, Jan 28, 2007
    #2
    1. Advertising

  3. Gary Wessle

    Ivan Novick Guest

    On Jan 28, 1:08 pm, Gary Wessle <> 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
    Ivan Novick, Jan 29, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacob
    Replies:
    7
    Views:
    415
    Dale King
    Oct 3, 2003
  2. dmoos AT esigma-systems DOT de

    implicit vs. explicit type conversion for string vs. (char *)

    dmoos AT esigma-systems DOT de, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    2,518
    Default User
    Jun 26, 2003
  3. Russell Reagan

    Implicit conversion is evil?

    Russell Reagan, Oct 27, 2003, in forum: C++
    Replies:
    2
    Views:
    319
    Russell Reagan
    Oct 27, 2003
  4. al
    Replies:
    2
    Views:
    5,615
    Ernst Murnleitner
    Jan 7, 2004
  5. Boris
    Replies:
    3
    Views:
    416
Loading...

Share This Page