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

A

Alfonso Morra

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
 
B

Bart

Alfonso Morra wrote:
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'.
 
A

Alfonso Morra

Bart said:
Alfonso Morra wrote:



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.
 
G

Gernot Frisch

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
 
J

Jack Klein

Alfonso Morra wrote:


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.
 
J

Jack Klein

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.
 
S

Stuart MacMartin

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
 
R

Ron Natalie

Jack said:
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.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top