implicit type casting

This is a discussion on implicit type casting within the C++ Programming forums, part of the General Programming Boards category; Suppose I have Code: int x; int y; double a; double z; a=<static_cast>(x)/y; // does this implicitly cast y to ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    implicit type casting

    Suppose I have
    Code:
    int x;
    int y;
    double a;
    double z;
    a=<static_cast>(x)/y;  // does this implicitly cast y to double
    a=z/y;                        // does this implicitly cast y to double
    I think it does. In Java it does but I am not sure about C++

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    static<cast>(x)/y;
    or
    Code:
    <static_cast>(x)/y;
    are wrong. Did you mean:
    Code:
    static_cast<double>(x)/y;
    or something else?

    If so, then I think yes, x is explicitly casted, and y is implicitly casted because the other operator argument is a double.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In Java it does but I am not sure about C++
    The result of an expression has the same type as the longest floatiest operand.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks for the replies

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    while we are on this topic.. i was taking a #2 ealier today.. and I was thinking about c++ programming.. and then i was thnking... "Is it necessary to typecast whilst using STL...???" Like.. if you divided two templated integers... would the template perform integer or floating point division...??
    Last edited by The Brain; 09-09-2004 at 10:35 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i was taking a #2 ealier today..
    Too much information.

    >Is it necessary to typecast whilst using STL...???
    If the situation warrants it, yes. But if you're using things properly these situations will be few and far between.

    >Like.. if you divided two templated integers...
    Can you show an example of what you mean?
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. type casting void *
    By Alexpo in forum C Programming
    Replies: 5
    Last Post: 06-23-2008, 03:05 AM
  2. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 06:09 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Type Casting for return statements, any gains??
    By lostInC in forum C++ Programming
    Replies: 7
    Last Post: 12-19-2002, 03:58 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21