470,870 Members | 1,826 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

Generic Pointer

I'am suffering with the concept of generic (void) pointer in C++. Please any body tell me the concept of generic pointer...
Nov 30 '07 #1
2 4161
671 Expert 512MB
Is there something specific about it that you don't understand? Perhaps you were reading an online reference, and didn't understand part of the description? Pointing out those specifics makes it easier for us to help you out.

With C and C++, everything must have a type. You can't say "I have a variable", and not be able to say if it is a char or int or a more complex structure. Now, with the aid of pointers, you can refer to variables. The problem is when you want a way of referring to something generically. For example, if you have a function that uses an argument. Your function deals with chars and ints differently. You just want the function to be able to accept something, which you'll resolve to a char or int yourself. This ambiguous something is the void pointer.

Void pointers are used heavily in C. Not so in C++, where there are syntactical constructs for generics, like function overloading and templates.
Nov 30 '07 #2
9,208 Expert Mod 8TB
What happened was:

In 1972 C was invented. In C function names must be unique so if you wrote a function to display an int:
Expand|Select|Wrap|Line Numbers
  1. void display(int arg)
  2. {
  3.      printf("%d", arg);
  4. }
then no one could use the name display for a funciton to display a float.

That forced function design to be not specific about the data and that caused the function to be written with a pointer to the data. A pointer to data whose type is not known is a void*. That is, it's a generic address. Now the function looks like:
Expand|Select|Wrap|Line Numbers
  1. void display(void* arg)
  2. {
  3.      printf("%d", *arg);
  4. }
which won't compile since you cannot dereference an address unless the address has a type. Like, int* dereferences to int so a void* would have to dereference to a void and a void means no type. Since all data must have type, the result is you are not allowed to dereference a void pointer.

Now you have to tell the function what the tyoe of the argument is by using a second argument. The function can then typecast the void pointer:
Expand|Select|Wrap|Line Numbers
  1. void display(void* arg, int parm)
  2.      int* temp_int;   
  3.      float* temp_float;
  4.      switch(parm)
  5.      {
  6.        case 1:
  7.            temp_int = (int*)arg;            
  8.            printf("%d", *temp_int);
  9.            break;
  10.       case 2:
  11.            temp_float = (float*)arg;
  12.            printf("%f", *temp_float);
  13.            break;
  14.        case 3:
  15.            /* you get the idea */
  17. }
This is really ugly and it forces you to change the function each time you have a new type of data to display. If the function has been distributed tio customers, then you have to upgrade all those customers.

C++ solves this problem with function overloading. Here the function name and the arguments must be unique rather than just the name.

oler1s says void* are not heavily used in C++. I would like to strengthen that by saying void* is not to be used in C++. void* is in C++ largely to be compatile with C and to allow you to use C functions if you really have to.
Nov 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by aurgathor | last post: by
1 post views Thread by Albert | last post: by
11 posts views Thread by redefined.horizons | last post: by
3 posts views Thread by Frederick Gotham | last post: by
1 post views Thread by interX | last post: by
2 posts views Thread by software | last post: by
7 posts views Thread by juerg.lemke | last post: by
32 posts views Thread by copx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.