473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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


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
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
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dmitry D | last post by:
Hi, I'm new to C++ (started learning in the beginning of this summer), and I have the following question (sorry if it sounds stupid): In many code samples and source files, I see NULL expression...
15
by: natespamacct | last post by:
Hi All, I'm not sure if I'm dealing with a C++ question or a compiler question, so please forgive me if I'm asking in the wrong spot. If so, maybe someone can direct me to more appropriate spot....
3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
4
by: Andreas Klimas | last post by:
hello, no many words, I will start with an example instead int foo(void *r) {...} void test(void) { int *x=0; foo(x); /* seems to me as valid */ }
33
by: jobo | last post by:
If I have the function: int f(int (*h)(int)) { return (*h)(13); } What exactly does (int (*h)(int)) do? So it's taking a pointer but what's with the two ints? Thanks.
4
by: Jeffrey Spoon | last post by:
Hello, I am trying to make a simple function that returns a pointer to another function. In my header file I've declared my function pointer: void (*pStateFunction) (void); //assume the function...
18
by: mdh | last post by:
May I ask the following. By K&R's own admission, the example used to describe function pointers is complex ( on P119). In addition, the use of casts has been stated by some on this group as...
28
by: junky_fellow | last post by:
Guys, Consider a function func(void **var) { /* In function I need to typecast the variable var as (int **) I mean to say, I need to access var as (int **) }
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.