Bl00d, this has nothing to do with polymorphism.
Quote:
Originally Posted by Soulstorm
Here is my first example:
int main (int argc, char * const argv[]) { int *p; int a = 100; void *v; p = &a; v = p; std::cout << v; return 0; }
The result I am getting is '0xbffff9c0' from the cout function. Actually, I had predicted it, but I don't know how can I access the contents of the void pointer. Can you help?
|
First a nitpick: it would help if you got your language straight, both in your description of output streams, and with usage of void pointers. Your description of cout as a function is woefully incorrect: cout is an object. When data is streamed to cout (eg cout << data) that data is written to a standard output device (normally the screen). I pick on this because, unless you can actually describe what streams are or what void pointers are, you have no hope of using them correctly. And using them correctly is a prerequisite to being able to use them (to use your description) "efficiently".
Now, to answer your question.....
C++ output streams (of which cout is one) provide an operator<< which takes a void pointer, and prints the address contained in the pointer to the stream.
It is not possible to "access" the contents of a void pointer directly as, by definition, a void pointer points at nothing in particular (i.e. there are no contents to access). The point (no pun intended) of void pointers is that they can point at anything. If you want to access whatever is being really pointed at, you (the programmer) need to know the type of what is really being pointed at. If you know that the pointer actually points at an int, convert the void pointer into a pointer to int. To get the value of that int, dereference the pointer to int.
In code, in your example, you would do this as;
std::cout << *(static_cast<int *>(v));
The static_cast converts v into a pointer to int. The * out the front dereferences that pointer to int, and extracts the int being pointed at.
One problem with void pointers is that they can be converted to anything. So you could also do this;
std::cout << *(static_cast<float *>(v));
which will attempt to print out the floating point value located at the address stored in v. This will sale past the compiler with no problems (casting a void pointer to any other pointer is allowed, and the compiler trusts you to do a valid conversion) but will print garbage because the value being pointed at is actually an int.
Comment to ponder on as you get more advanced: in practice, printing an int as a float will often yield garbage output. Technically, it is actually undefined behaviour (as that term is defined in the C++ standard), and a program crash is an equally correct outcome.
Quote:
Originally Posted by Soulstorm
Another example:
void f(void *p){ printf("%s",p); } int main (int argc, char * const argv[]) { char p[] = "hello!"; f(p); return 0; }
The above code works as expected. It shows 'hello'.
|
Minor nitpick: Actually, it shows "Hello", not 'Hello'.
The reason is that the "%s" format specifier you have supplied explicitly tells the printf() function that p is actually a C-style string.
Quote:
Originally Posted by Soulstorm
But if I replace the printf fuunction with the cout:
void f(void *p){ cout << p; } int main (int argc, char * const argv[]) { char p[] = "hello!"; f(p); return 0; }
I get an address! (0xbffff9b8).
Can you please explain to me why this is happenning (in both examples) and what can I do to dereference a void pointer so that I can access the elements it points to?
|
The reason is that you haven't told the stream to print a string; you've told it to print out a void pointer, and it is doing exactly that: it is printing the address stored in v.
If you want the stream to print out the string "Hello", you need to convert the void pointer into a pointer to char. When it receives a pointer to char, the stream operators (by convention) treat it as a C-style string (an array of char, with the last one being a char with value zero.
void f(void *p){
cout << static_cast<char *>(p);
}