How know smth is a pointer to an object instead of an object?

This is a discussion on How know smth is a pointer to an object instead of an object? within the C++ Programming forums, part of the General Programming Boards category; If there's a define macro that takes an object, how can it figure out if the object passed is the ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    How know smth is a pointer to an object instead of an object?

    If there's a define macro that takes an object, how can it figure out if the object passed is the actual object or a pointer to the object?

    Pseudo code:

    Code:
    #define handleTest( obj )                                             \
         Test* t;                                                         \
                                                                          \
         /* want to do something like this */                             \
         if ( obj isOfType pointer ) t = dynamic_cast<Test*> obj;         \
         else t = &( ( Test ) obj );
    Last edited by 6tr6tr; 04-23-2008 at 10:31 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is such a thing as typeof, if you only are expecting one type of object. Although heaven knows it might just be easier to write real functions instead.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,261
    I think I've said this to you about nine times: Templates + Traits.

    Soma

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Code:
    template <typename T>
    bool is_a_pointer(T obj_p)
    {
        return false;
    }
    
    template <typename T>
    bool is_a_pointer(T *ptr_p)
    {
        return true;
    }
    Or use a full-blown traits pattern.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It still makes me wonder, could the compiler handle t being two different types at the same time?

    Code:
         if ( obj isOfType pointer ) t = dynamic_cast<Test*> obj;         \
         else t = &( ( Test ) obj );
    Why not simply require that t shall always be a pointer (I can't see how you'd suddenly end up with a Test or a Test* and not know which it is)?

    Edit: well apparently obj is of unknown type here, but still...
    Last edited by anon; 04-23-2008 at 01:41 PM.
    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).

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by anon View Post

    Why not simply require that t shall always be a pointer (I can't see how you'd suddenly end up with a Test or a Test* and not know which it is)?
    I was wondering that myself. I'm also curious, in a morbid sort of way, to see 6tr6tr's code after this ongoing exercise in mental Twister to subvert traditional best practices.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. trouble with pointer to object
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2003, 03:10 PM

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