Quick cast question

This is a discussion on Quick cast question within the C++ Programming forums, part of the General Programming Boards category; Which C++ cast is closest to the C style (t) e one? I vaguely remember the C++ D&E saying t'is ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Quick cast question

    Which C++ cast is closest to the C style (t) e one? I vaguely remember the C++ D&E saying t'is static_cast but I can't remember and don't have the book here with me.
    Last edited by cboard_member; 05-05-2006 at 09:34 AM. Reason: t'was should've been t'is, dag nabbit!
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Yes, I'd say static_cast is closest, since it can be used in several different situations.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    I thought it was reinterpret_cast
    http://publib.boulder.ibm.com/infoce...rpret_cast.htm
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Hmm ok thanks guys.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Code:
    #include <iostream>
    
    int main()
    {
        float f = 10.0f;
        int i1 = (int) f;
        int i2 = static_cast<int>(f);
        int i3 = reinterpret_cast<int>(f); // error cannot convert float to int
        std::cout << i1 << '\n';
        std::cout << i2 << '\n';
        std::cout << i3 << '\n';
    }

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    \/
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick question (adding characters)
    By Cactus in forum C Programming
    Replies: 2
    Last Post: 09-24-2005, 03:54 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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