checking cast from void*

This is a discussion on checking cast from void* within the C++ Programming forums, part of the General Programming Boards category; Hi today I mistaped a cast from void* to the wrong type and everything was fine. How can I check/realize ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    checking cast from void*

    Hi
    today I mistaped a cast from void* to the wrong type
    and everything was fine.
    How can I check/realize that I am using the wrong class
    or prevent cases like the one below?

    Code:
    void main()
    {
    
        void *ptr;
        MyObject2* p2 = &obj2;
        ptr = p2;
    
        // here I casted to MyObject1 a pointer to
        // MyObject2...
        MyObject1* p1 = static_cast< MyObject1* >( ptr );
    
       // ...and he works fine! It writes 20: 
       // how to prevent this?
        p1->Mutator1( 20 );
    
    }
    
    class MyObject1
    {
    public:
        void Mutator1( int a )
        {
            std::cout << a << std::endl;
         }
    };
    
    class MyObject2
    {
    public:
        void Mutator2( int a )
        {
            std::cout << a << std::endl;
            }
    };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think dynamic_cast checks that it makes sense.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Quote Originally Posted by manustone
    today I mistaped a cast from void* to the wrong type
    and everything was fine.
    How can I check/realize that I am using the wrong class
    or prevent cases like the one below?
    Don't involve a pointer to void in the first place.

    Quote Originally Posted by tabstop
    I think dynamic_cast checks that it makes sense.
    dynamic_cast would only make sense where polymorphic types are involved.
    Last edited by laserlight; 07-14-2009 at 05:10 PM.
    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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by laserlight View Post
    Don't involve a pointer to void in the first place.


    dynamic_cast would only make sense where polymorphic types are involved.
    IAW the first sentence.

    And I ended up checking the dynamic_cast -- apparently you can't dynamic_cast void* to anything at all (compile error), so that's a washout.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    IAW??
    "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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cpjust View Post
    IAW??
    I don't know. Do you agree with the first sentence?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I think he's wondering (as I am) what IAW means.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    When you use a void* then it already means "I don't want no stinking compiler helping me about getting my types right. I know better".

    So you can't keep using void* and expect help from the compiler.

    You could describe why you are using a void* in the first place because one should be needed quite rarely in C++ programs.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    "I Agree With"

    Soma

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Since no one has mentioned void main yet...
    SourceForge.net: Void main - cpwiki

    Also, to use functions that accepts any types, take a look at polymorphism and templates. No need for void*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Lightbulb

    Hi guys!
    First of all I wish to thanks everyone for their reply.
    I used void* becasue for me means somekind of everything
    ( last post got the right point )!
    After my post, few days ago I found a very interesting class in boost library called boost::any and boost::any_cast.
    Pleae, have a look to it; it is a container for everything that has a copy-constructor ( I hope to rember right ).
    Have a nice day!
    ManuStone

    Code:
    void CastToRightType( boost::any& obj )
    {
           MyObject1* pnt = boost::any_cast<  MyObject1* >( obj );
    }
    void main()
    {
        MyObject2 obj2;
        MyObjec2 obj1;
        boost::any anyObject1 = &obj1;
        boost::any anyObject2 = &obj2;
    
       CastToRightType( anyObject1 );    // fine
       CastToRightType( anyObject2 )     // exception
    }

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Change your void main and read up about templates.
    Code:
    template<typename T> void CastToRightType( T& obj )
    {
           
    }
    
    // Use int main FFS
    int main()
    {
        MyObject2 obj2;
        MyObjec2 obj1;
    
       CastToRightType( obj1 );    // fine
       CastToRightType( obj2 );    // fine
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The implementation of Boost.Any is pretty simple, compared to other Boost libraries. You could simply look at its code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 02:32 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 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