Help - converting void* to string&

Hi,

I have 3 functions that receive different types of parameters, but at some point preform the same actions..
seek(int key)
seek(char* key)
seek(string& key)


I call the functions on seek(void* key, int type)
1
2
3
4
5
6
7
8
9
	void seek(void* key, int type)
	{
		if (type==0)   // if key type is int
			seek(*(int*)key);
		if (type==1)   // if key type is char*
			seek((char*)(key));
		if (type==2)   // if key type is string&
			seek((string&)key);
	}


now, on function seek(string& key) it writes me "Bad ptr" regarding key -
why? How do I convert void* to string& ?

Thanks in advance...
Last edited on
Disregarding the fact that there are better ways to do this, the answer to your question would be the following (untested), assuming key is of type std::string*:

seek(*(std::string*)(key));
Thanks a lot!

btw, what better ways for examples?
You could use a templated function/class, and depending on what you are trying to do, it is not inconceivable that you could use polymorphism. The use of void* is from the days of C. C++ has greatly reduced the need of using void* and has made the code more readable.
Topic archived. No new replies allowed.