By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,908 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,908 IT Pros & Developers. It's quick & easy.

Not nice but (question on void* and this pointer)

P: n/a
Straight of the bat, I'll admit this is not a nice solution, it is
dangerous (not type safe) etc,etc, I know what the perils are. I don't
need a lecture on why what I'm doing is perilous - that is not what I'm
after, I want to know *how* I can do it, not *why* I should not do it.

I have an opaque abstract data type that has a void* (yes I know its
ugly) as one of its members (actually the member is a union - but that's
is of little consequence here). I want to be able to use the void
pointer to store different things - including pointers to objects -
based on information stored elsewhere, I am always able to ascertain
what the void* is pointing to and cast it appropriately (I know about
the evils of c-style casts and static casts - but still, bear with me).

I suspect that a pointer is a pointer (nothing more or less) and that I
should be able to store pointers to an object as void* (however abhorent
it may seem to purists). Finally, if I need to return a pointer to an
object, do I return "this" or "*this"?

I look forward to informed responses

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Alfonso Morra wrote:
<snip>
I suspect that a pointer is a pointer (nothing more or less) and that I
should be able to store pointers to an object as void* (however abhorent
it may seem to purists). Finally, if I need to return a pointer to an
object, do I return "this" or "*this"?


It wasn't necessary to post such a long message for such a short
question. Any pointer type can be converted to void*. 'this' is a
pointer to the object while '*this' is a reference to the object.
Therefore, you should use 'this'.

Jul 23 '05 #2

P: n/a


Bart wrote:
Alfonso Morra wrote:
<snip>
I suspect that a pointer is a pointer (nothing more or less) and that I
should be able to store pointers to an object as void* (however abhorent
it may seem to purists). Finally, if I need to return a pointer to an
object, do I return "this" or "*this"?

It wasn't necessary to post such a long message for such a short
question. Any pointer type can be converted to void*. 'this' is a
pointer to the object while '*this' is a reference to the object.
Therefore, you should use 'this'.


Thanks Bart. I was a bit paranoid that I'd get flamed with tons of
people telling me why i shouldn't be doing this. Turned ou not to be the
case. LOL. Many thanks. I just needed validation of what i was thinking
of doing.

Jul 23 '05 #3

P: n/a
Thanks Bart. I was a bit paranoid that I'd get flamed with tons of
people telling me why i shouldn't be doing this. Turned ou not to be
the case. LOL. Many thanks. I just needed validation of what i was
thinking of doing.

But... why are you using a void*? can't you use a base class for all
the things you want to point to?
;)

-Gernot
Jul 23 '05 #4

P: n/a
On 19 Jul 2005 05:12:34 -0700, "Bart" <ba***********@gmail.com> wrote
in comp.lang.c++:
Alfonso Morra wrote:
<snip>
I suspect that a pointer is a pointer (nothing more or less) and that I
should be able to store pointers to an object as void* (however abhorent
it may seem to purists). Finally, if I need to return a pointer to an
object, do I return "this" or "*this"?


It wasn't necessary to post such a long message for such a short
question. Any pointer type can be converted to void*. 'this' is a
pointer to the object while '*this' is a reference to the object.
Therefore, you should use 'this'.


Not correct about void *, see my reply to the OP.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #5

P: n/a
On Tue, 19 Jul 2005 11:54:56 +0000 (UTC), Alfonso Morra
<sw***********@the-ring.com> wrote in comp.lang.c++:
Straight of the bat, I'll admit this is not a nice solution, it is
dangerous (not type safe) etc,etc, I know what the perils are. I don't
need a lecture on why what I'm doing is perilous - that is not what I'm
after, I want to know *how* I can do it, not *why* I should not do it.

I have an opaque abstract data type that has a void* (yes I know its
ugly) as one of its members (actually the member is a union - but that's
is of little consequence here). I want to be able to use the void
pointer to store different things - including pointers to objects -
based on information stored elsewhere, I am always able to ascertain
what the void* is pointing to and cast it appropriately (I know about
the evils of c-style casts and static casts - but still, bear with me).

I suspect that a pointer is a pointer (nothing more or less) and that I
should be able to store pointers to an object as void* (however abhorent
it may seem to purists). Finally, if I need to return a pointer to an
object, do I return "this" or "*this"?

I look forward to informed responses


You are correct that a pointer to any object type in C++ may be
converted to a pointer to void, and later converted back to a pointer
to the original type (with suitable casts), and the result will still
point to the same object.

You are not quite correct when you say "a pointer is a pointer". C++
has three basic types of pointer, two inherited from C:

1. pointer to object

2. pointer to function (C), or free-standing or static member
function (C++)

3. pointer to non-static member function.

Of the three, only type 1 is convertible to and from pointer to void.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #6

P: n/a
>But... why are you using a void*? can't you use a base class for all the things you want to point to?

I'll take that as a serious question, considering the long preamble to
this thread...

Very often, no.

A common trick is to write a type unsafe class, and write a typesafe
interface. Can be used e.g. to reduce template instantiation, or to
manage memory. In our application, this is essential for some classes.

Using a common base class assumes:
- all objects can and should inherit from a common base class
- all classes that currently don't share a common base class can be
made to do so (some might be from libraries)
- all pointers are to objects and not base types
It's unreasonable for a utility class to impose such restrictions.

Stuart

Jul 23 '05 #7

P: n/a
Jack Klein wrote:
You are not quite correct when you say "a pointer is a pointer". C++
has three basic types of pointer, two inherited from C:

1. pointer to object

2. pointer to function (C), or free-standing or static member
function (C++)

3. pointer to non-static member function.

Of the three, only type 1 is convertible to and from pointer to void.


4. pointer to non-static data member.
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.