Archived

This topic is now archived and is closed to further replies.

Nacho

Using void pointers

Recommended Posts

Hi! I´ve got a very simple question about void pointers: Where can you apply them in your programs? I´ve been reading the MSDN and the only information provided is that they can point to any kind of variable and that they can only be dereferenced if they are casted to another type. I tought that they may be applied to a situation like the following one (altough I consider that this example is pretty useless): const int INT = 1; const int FLOAT = 2; const int DOUBLE = 3; void Function(void *ptvoid,int type) { switch(type) { case INT: { *(int*)ptvoid = 5; break; } case FLOAT: { *(float*)ptvoid = 10.5f; break; } } return; } int main() { int vari = 0; float varf = 0.0f; Function(&varf,FLOAT); Function(&vari,INT); return 1; } Is that correct or did I completely misunderstand the purpose of this kind of pointers? All replies are welcome, thanks!

Share this post


Link to post
Share on other sites
Does it compile? It looks like it will. And yes that shows one use for them. void pointers are more of a C thing than a C++ thing. If you dig through the windows headers, you''ll find that many of the handle types are really just void pointers - hwnd, hinstance, handle etc.

Share this post


Link to post
Share on other sites
Well I would think it would be usefull for a function in a game engine... like you could have an AI object that is given another object via the void pointer and then the function would find the class type and take appropiate action...

Share this post


Link to post
Share on other sites
The only reason I declare void pointers is to cast them later on. Say you have two classes that are dependent on each other *(pointers to the other type in the class definition). One class has to be defined first, but it can''t have a pointer to the second class since that hasn''t been defined yet. So in the first class I use void pointers and cast them as needed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
well if you hit that problem you can just prototype the class

class A;
class B;

class A
{
public:
B tmpB;
};

Share this post


Link to post
Share on other sites
quote:
Original post by Psybr
The only reason I declare void pointers is to cast them later on. Say you have two classes that are dependent on each other *(pointers to the other type in the class definition). One class has to be defined first, but it can''t have a pointer to the second class since that hasn''t been defined yet. So in the first class I use void pointers and cast them as needed.

Use a forward reference instead:

  
class Foo;

class Bar
{
public:
//...

private:
Foo* fooPtr;
};

void pointers should generally be avoided in C++. The above AP''s example won''t work, since he''s creating an actual instance of the other class. Forward referencing only works with pointers.




"The churches used to win their arguments against atheism, agnosticism, and other burning issues by burning the ismists, which is fine proof that there is a devil but hardly evidence that there is a God."
Ben Lindsey and Wainwright Evans

Share this post


Link to post
Share on other sites
quote:
Original post by Ignacio Liverotti
Hi! I´ve got a very simple question about void pointers: Where can you apply them in your programs?

Legacy interoperability.

void pointers were a method for type agnosticism in C, and since C++ is largely a superset of C it retains that functionality. C++ imposes the restriction that casts from void pointer to any other pointer type must be explicit, but other than that they function very much like in C. C++, however, provides superior mechanisms for type-agnostic programming (templates) and run-time type information as well (typeid), making the use of void pointers in a modern C++ design unnecessary and undesirable.

Basically, the only need for void pointers is to interface with older C and (bad) C++ code.

Incidentally, your example is better solved with overloading. C++ tries to emphasize type safety, and code like that circumvents it at no advantage.

Share this post


Link to post
Share on other sites
Wow! Too many replies! Thanks to all of you people! And yes, the code compiles, but I just wanted to know where can void pointers be applied. Thanks Oluseyi for your reply. You´re right, an overloaded Function() would be a better solution, but can I apply void pointers to any specific situation? Thanks!

Share this post


Link to post
Share on other sites
I''m no network programming expert, but if I remember correctly, DirectPlay requires you to cast the data sent to a (void*) pointer. Even with all the OO programming in the world, you can''t avoid losing type info when going over a network, so you have to encode it somehow.

Cédric

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
void pointers are not needed. Use function overloading, templates, or inheritance. Basically void* is a thing to use with casting, and casting is a thing to use when your language does not have the features you need.

Share this post


Link to post
Share on other sites
*whispers so the newbies can''t hear*
Actually void pointers can be useful in preventing template bloat. When you have parts of a template class whose operation don''t depend on the parameterised type, it can be useful to cast them to void* so that you only get one instance of certain function in your entire program, rather than a load of identical ones for each type. Probably unnecessary, not always undesirable.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
@Ignacio Liverotti :

I emailed you an article that should help you with your void pointers.

I emailed it to the address in your profile.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
@Ignacio Liverotti :

I emailed you an article that should help you with your void pointers.

I emailed it to the address in your profile.


Yes, I´ve just received it! During my next session (I have a dial up connection) I´ll tell you about it. Thanks!

Share this post


Link to post
Share on other sites
Just a little question...

In the VCL, there is TRadioButton and TCheckBox....
Now I once had a problem where I wanted to read the boolean checked in both of these classes, without dublicate code..
I tried many soloutions, like making a derived class of the last class they had incommon (TComponent) which contained a checked bool, and then casting my RadioButtons/CheckBoxes to this class before sending them through my function... This caused an Access Violation..
Eventualy I wound up with overloading the function to take both of these types and then dublicating the code...
Thinking of it, wouldn''t I be able to cast my classes to void pointers, and then to the class with the checked flag?

Now I just wish I had known of void pointers at that point

P.S. While we''re at solving the above problem, would it be able to derive a class from TRadioButton and TCheckBox, and using this class as the function paramenter?

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
@Ignacio Liverotti :

I emailed you an article that should help you with your void pointers.

I emailed it to the address in your profile.


I''m interested in seeing that article too. Can you share the url or email to me as well? TIA.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Coward
Just a little question...

In the VCL, there is TRadioButton and TCheckBox....
Now I once had a problem where I wanted to read the boolean checked in both of these classes, without dublicate code..
I tried many soloutions, like making a derived class of the last class they had incommon (TComponent) which contained a checked bool, and then casting my RadioButtons/CheckBoxes to this class before sending them through my function... This caused an Access Violation..
Eventualy I wound up with overloading the function to take both of these types and then dublicating the code...
Thinking of it, wouldn''t I be able to cast my classes to void pointers, and then to the class with the checked flag?

Now I just wish I had known of void pointers at that point

P.S. While we''re at solving the above problem, would it be able to derive a class from TRadioButton and TCheckBox, and using this class as the function paramenter?


Well, yes, a void pointer would be a solution. As well as function overloading and templates. 3 solutions to your problem.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by LessBread
I''m interested in seeing that article too. Can you share the url or email to me as well? TIA.


No problem, I emailed you the article. In retrospect, I need to go back and add double pointers.

I also wrote a XML wrapper in the past 3 days and I''m getting feedback about it too. However, to use it, you have to know how to convert C-Style strings.

-James

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Well, yes, a void pointer would be a solution. As well as function overloading and templates. 3 solutions to your problem.


Ok, thanks, I feared using void pointers too would give me an Access Violation...

Anyway, would you care to show me how the template soloution would look like?

EDIT: I'm abit offtopic here, right?

[edited by - Coward on July 28, 2002 9:17:03 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Coward
EDIT: I''m abit offtopic here, right?



Um, yeah, but don''t sweat it, the "Off Topic" police aren''t quite as active here as they are at say, comp.lang.c. I think Ignacio got his answer too so ask away - however - by asking your question in a separate thread you might get answers from a wider variety of sources - without the filter imposed by Ignacio''s question.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Coward
Ok, thanks, I feared using void pointers too would give me an Access Violation...

Anyway, would you care to show me how the template soloution would look like?

EDIT: I''m abit offtopic here, right?



  
class FirstClass
{
bool boolvalue;
int someValue;
}

class secondclass
{
bool boolvalue;
float someValue;
}

template <typename T, class G>
void SomeFunction( T tInput, G gInput)
{
if(gInput.boolvalue == true)
{
gInput.someValue += tInput;
}
}


Something like that. This is off the top of my head, but you would be able to pass any type T and any class to the function. The function assumes that any class passed to it has boolValue and someValue. I would really look up articles on the web to find out about templates and when you are comfortable with templates, learn STL.

*Before anyone flames me, This is off the top of my head. I did not test this code AT ALL*
quote:

Prince Wang''s programmer was coding software. His fingers danced upon the
keyboard. The program compiled without an error message and ran like a
gentle wind.

"Excellent!" the Prince exclaimed. "Your technique is faultless!"

"Technique?" said the programmer, turning from his terminal, "What I follow
is Tao -- beyond all techniques! When I first began to program, I would see
before me the whole problem in one mass. After three years, I no longer saw
this mass. Instead, I used subroutines. But now I see nothing. My whole
being exists in a formless void. My senses are idle. My spirit, free to
work without a plan, follows its own instinct. In short, my program writes
itself. True, sometimes there are difficult problems. I see them coming, I
slow down, I watch silently. Then I change a single line of code and the
difficulties vanish like puffs of idle smoke. I then compile the program.
I sit still and let the joy of the work fill my being. I close my eyes for
a moment and then log off."

Share this post


Link to post
Share on other sites