Nasty C-style cast

This is a discussion on Nasty C-style cast within the C++ Programming forums, part of the General Programming Boards category; Code: return ((Foo*)bar)->func(); How would I cast this with reinterpret_cast? I tried the following without success; Code: return reinterpret_cast<Foo*>(bar)->func();...

  1. #1
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135

    Nasty C-style cast

    Code:
    return ((Foo*)bar)->func();
    How would I cast this with reinterpret_cast? I tried the following without success;

    Code:
    return reinterpret_cast<Foo*>(bar)->func();

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    I'd say that that depends on what is the type of bar, and how it is related to Foo.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    And what does the extra parentheses do in the first snippet?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by Tux0r
    And what does the extra parentheses do in the first snippet?
    So that the cast applies to bar, not bar->func().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Ok

    I'm gonna try solving the problem again...

    edit: Didn't work so I just put a static_cast there... I still don't get why reinterpret_cast didn't work though.
    Last edited by Tux0r; 07-21-2009 at 04:28 AM.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe you need dynamic_cast or const_cast? Without knowing what the types are and how they're related, it's hard to say.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 02:32 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 09:14 PM

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