Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Storing Pointer in a Class Hierarchy

Reply
Thread Tools

Storing Pointer in a Class Hierarchy

 
 
William Djaja Tjokroaminata
Guest
Posts: n/a
 
      02-12-2004
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
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      02-12-2004
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?Tes...UserInterfaces


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-12-2004

"William Djaja Tjokroaminata" <(E-Mail Removed)> wrote in message
news:c0g6ck$nis$(E-Mail Removed)...
> 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


 
Reply With Quote
 
William Djaja Tjokroaminata
Guest
Posts: n/a
 
      02-12-2004
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 <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
User Images: Storing in Files VS Storing in Database Jonathan Wood ASP .Net 1 06-02-2008 05:56 PM
pasing an array pointer and storing as a class member stephen b C++ 14 10-17-2007 05:43 PM
storing pointer vs storing object toton C++ 11 10-13-2006 11:08 AM
Storing pointer to instance of class with type conversion operator Whitney Kew C++ 10 12-07-2004 01:28 AM
Invoking the Constructor of the Top Most Class in the Hierarchy from the Bottom most class H.MuthuKumaraRajan Java 3 02-04-2004 01:33 PM



Advertisments