Storing Pointer in a Class Hierarchy

Discussion in 'C++' started by William Djaja Tjokroaminata, Feb 12, 2004.

  1. Hi,

    As I am interfacing my code in C++ with a scripting language which is
    written in C, I need to store pointers to objects in a class hierarchy:

    ParentClass *obj1;
    obj1 = new ParentClass (...);
    ... do things with obj1...
    STORE_POINTER (scriptObj1, obj1);

    DerivedClass *obj2;
    obj2 = new DerivedClass (...);
    ...do things with obj2...
    STORE_POINTER (scriptObj2, obj2);

    However, when I try to retrieve an object, as I don't know which class the
    object belongs to, I always use the base class (in this code segment I
    just need the functionalities related to the ParentClass):

    ParentClass *objN;
    RETRIEVE_POINTER (scriptObjN, ParentClass, objN);
    ...do things with objN...

    where STORE_POINTER and RETRIEVE_POINTER are some utility macros provided
    by the scripting language.

    I ran the code and I got a segmentation fault. I also notice that when
    I printed out a pointer and cast it to different classes in the hierarchy,
    I got different values:

    cout << "pointer as parent = " << (ParentClass*) this << endl;
    cout << "pointer as derived = " << (DerivedClass*) this << endl;

    So my question is, in C++, is it unsafe to type cast a pointer to a
    derived class to a pointer to a base class? (The other way around never
    makes sense, of course.) If it is unsafe, what is the best way to solve
    the above problem? Thanks.

    Regards,

    Bill
     
    William Djaja Tjokroaminata, Feb 12, 2004
    #1
    1. Advertising

  2. William Djaja Tjokroaminata

    Phlip Guest

    William Djaja Tjokroaminata wrote:

    > However, when I try to retrieve an object, as I don't know which class the
    > object belongs to, I always use the base class (in this code segment I
    > just need the functionalities related to the ParentClass):
    >
    > ParentClass *objN;
    > RETRIEVE_POINTER (scriptObjN, ParentClass, objN);
    > ...do things with objN...
    >
    > where STORE_POINTER and RETRIEVE_POINTER are some utility macros provided
    > by the scripting language.
    >
    > I ran the code and I got a segmentation fault. I also notice that when
    > I printed out a pointer and cast it to different classes in the hierarchy,
    > I got different values:
    >
    > cout << "pointer as parent = " << (ParentClass*) this << endl;
    > cout << "pointer as derived = " << (DerivedClass*) this << endl;


    Never use a C-style cast.

    In your case, if ParentClass contains virtual things,
    dynamic_cast<DerivedClass*>(that) will return NULL if '*that' is not a
    DerivedClass.

    --
    Phlip
    http://www.xpsd.org/cgi-bin/wiki?TestFirstUserInterfaces
     
    Phlip, Feb 12, 2004
    #2
    1. Advertising

  3. "William Djaja Tjokroaminata" <> wrote in message
    news:c0g6ck$nis$...
    > Hi,
    >
    > As I am interfacing my code in C++ with a scripting language which is
    > written in C, I need to store pointers to objects in a class hierarchy:
    >
    > ParentClass *obj1;
    > obj1 = new ParentClass (...);
    > ... do things with obj1...
    > STORE_POINTER (scriptObj1, obj1);
    >
    > DerivedClass *obj2;
    > obj2 = new DerivedClass (...);
    > ...do things with obj2...
    > STORE_POINTER (scriptObj2, obj2);
    >
    > However, when I try to retrieve an object, as I don't know which class the
    > object belongs to, I always use the base class (in this code segment I
    > just need the functionalities related to the ParentClass):
    >
    > ParentClass *objN;
    > RETRIEVE_POINTER (scriptObjN, ParentClass, objN);
    > ...do things with objN...
    >
    > where STORE_POINTER and RETRIEVE_POINTER are some utility macros provided
    > by the scripting language.
    >
    > I ran the code and I got a segmentation fault. I also notice that when
    > I printed out a pointer and cast it to different classes in the hierarchy,
    > I got different values:
    >
    > cout << "pointer as parent = " << (ParentClass*) this << endl;
    > cout << "pointer as derived = " << (DerivedClass*) this << endl;
    >
    > So my question is, in C++, is it unsafe to type cast a pointer to a
    > derived class to a pointer to a base class?


    That is never unsafe, I fact it so safe that you don't need a cast. In fact
    its wrong to use a cast.

    You should not be using that fact that the pointer value changes as evidence
    that the technique is unsafe. Sometimes the pointer changing value is what
    is supposed to happen.

    > (The other way around never
    > makes sense, of course.)


    Never make sense, why not? In C++ one would use dynamic_cast or static_cast
    for exactly this purpose.

    john
     
    John Harrison, Feb 12, 2004
    #3
  4. Hi,

    Thanks for the reply. Can someone point out how to use static/dynamic
    cast correctly in relation with STORE_POINTER and RETRIEVE_POINTER (which
    were written in C)?

    Regards,

    Bill
    =========================================================================
    Phlip <> wrote:
    > Never use a C-style cast.


    > In your case, if ParentClass contains virtual things,
    > dynamic_cast<DerivedClass*>(that) will return NULL if '*that' is not a
    > DerivedClass.
     
    William Djaja Tjokroaminata, Feb 12, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H.MuthuKumaraRajan
    Replies:
    3
    Views:
    490
    H.MuthuKumaraRajan
    Feb 4, 2004
  2. Whitney Kew
    Replies:
    10
    Views:
    599
    Whitney Kew
    Dec 7, 2004
  3. toton
    Replies:
    11
    Views:
    738
    toton
    Oct 13, 2006
  4. stephen b
    Replies:
    14
    Views:
    528
  5. Jonathan Wood
    Replies:
    1
    Views:
    530
    Jonathan Wood
    Jun 2, 2008
Loading...

Share This Page