Re: Can I check the type of a void pointer
From: Martijn Lievaart (m_at_remove.this.part.rtij.nl)
Date: 12/02/03
- Next message: Peter van Merkerk: "Re: classes, strings, learning in VS.NET"
- Previous message: Xenos: "Re: OT: console program"
- In reply to: Ron Natalie: "Re: Can I check the type of a void pointer"
- Next in thread: tom_usenet: "Re: Can I check the type of a void pointer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 02 Dec 2003 16:12:26 +0100
On Tue, 02 Dec 2003 09:57:28 -0500, Ron Natalie wrote:
>
> "Damon" <so_excited@excite.com> wrote in message news:159c8bc6.0312020650.178ee39@posting.google.com...
> ]
>>
>> So question to all the experts, can I check the type of a void pointer
>> or use some guaranteed way to elicit an exception without going
>> segmentation fault? BTW, I'm using g++ v3.2.2. I know that if i try to
>> delete the instances of the template class that was cast wrongly, it
>> segfault but I can't trap it!
>>
> No, once you cast a pointer to void*, you're only options are:
> check it against the null pointer constant.
> cast it back to exactly what it was before.
>
> The only solution I can suggest is to make your template class inherit
> from a common (public) base class and use that rather than void*. If
> the base class is polymorphic (has a virutal function), then you can use
> dynamic_cast to validate it against any particular derived class.
Adding to that, if you /need/ a void pointer (say for some C-ish
callback), you can use this technique as well. Cast to the base class.
Cast to void. Pass pointer. Cast back to base class. Use polymorphism
(virtual functions) or dynamic_cast<> from there.
HTH,
M4
- Next message: Peter van Merkerk: "Re: classes, strings, learning in VS.NET"
- Previous message: Xenos: "Re: OT: console program"
- In reply to: Ron Natalie: "Re: Can I check the type of a void pointer"
- Next in thread: tom_usenet: "Re: Can I check the type of a void pointer"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|