IS_A boolean operator?

This is a discussion on IS_A boolean operator? within the C++ Programming forums, part of the General Programming Boards category; Is "IS_A" a boolean operator? I want to find out what type of class a pointer is pointing to, and ...

  1. #1
    stolee
    Guest

    Question IS_A boolean operator?

    Is "IS_A" a boolean operator?

    I want to find out what type of class a pointer is pointing to, and I don't know how... tell me if I'm right or wrong and inform me correctly please!

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    dynamic_cast

    dynamic_cast is exclusively used with pointers and references to objects. It allows any type-casting that can be implicitly performed as well as the inverse one when used with polymorphic classes, however, unlike static_cast, dynamic_cast checks, in this last case, if the operation is valid. That is to say, it checks if the casting is going to return a valid complete object of the requested type.

    Checking is performed during run-time execution. If the pointer being casted is not a pointer to a valid complete object of the requested type, the value returned is a NULL pointer.

    class Base { virtual dummy(){}; };
    class Derived : public Base { };

    Base* b1 = new Derived;
    Base* b2 = new Base;
    Derived* d1 = dynamic_cast<Derived*>(b1); // succeeds
    Derived* d2 = dynamic_cast<Derived*>(b2); // fails: returns NULL
    from

    http://www.cplusplus.com/doc/tutorial/tut5-4.html
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    run time information

    Here is a sample of how this can be accomplished using the MFC.
    This will only work with the classes that are derived from CObject. You might get an idead of how to implement this without the MFC. I have no idea of how they do it... just thought you might be interested...

    BTW - You can use MFC in a console app...

    // in .H file
    class CPerson : public CObject
    {
    DECLARE_DYNAMIC( CPerson )
    public:
    CPerson(){};

    // other declaration
    };


    // in .CPP file
    IMPLEMENT_DYNAMIC( CPerson, CObject )


    void SomeFunction(void)
    {
    CObject* pMyObject = new CPerson;

    if(pMyObject->IsKindOf( RUNTIME_CLASS( CPerson ) ) )
    {
    //if IsKindOf is true, then cast is all right
    CPerson* pmyPerson = (CPerson*) pMyObject ;
    ...
    delete pmyPerson;
    }
    ...
    delete [MyObject];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean operator "or"
    By Amyaayaa in forum C++ Programming
    Replies: 14
    Last Post: 02-15-2008, 06:56 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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