Storing Pointer in a Class Hierarchy

  • Thread starter William Djaja Tjokroaminata
  • Start date
W

William Djaja Tjokroaminata

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
 
P

Phlip

William said:
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.
 
J

John Harrison

William Djaja Tjokroaminata said:
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
 
W

William Djaja Tjokroaminata

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
=========================================================================
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top