Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: Dr. Nick
I'm fairly familiar with pointers in C++, but ran into a problem today. I have a function that I want to be able to take an input of any datatype. As an example, a simple one to find the index of an item in an array:
=================================================================
int find(void *array, void *item, int sizeofArray)
{
for (int i=0; i<sizeofArray; i++)
if (array[i] == *item)
return i;return -1;
}The problem with this is that the compiler has no idea what the size of the void pointers are. I thought I could fix this by adding another parameter to the function, then based on that, cast the void pointers to a definite type:
int find(void *array, void *item, int sizeofArray, int sizeofData)
{
switch(sizeofData)
{
case sizeof(int): // Convert void* to int*
case sizeof(char): // Convert void* to int*
// ...
default: return -1;
}for (int i=0; i<sizeofArray; i++)
if (array[i] == *item)
return i;return -1;
}The problem above is that I can't figure out how to either completely change the type of the pointers 'array' and 'item', or create a new set of pointers which will let the code work.
Any ideas? (Or questions as to what the heck I'm talking about more likely...)
Thanks.

I don't know about casting a void to something else, but why not just overload the function to allow for the finite number of possibilities?
Chi
They mostly come at night...mostly

You might want to consider using templates. That way the data type can be any numeric date type. Here is an example:
template<class T> int find(T *array, T *item, int sizeofArray)
{
for (int i=0; i<sizeofArray; i++)
if (array[i] == *item)
return i;
return -1;
}I believe the syntax is correct. I can't test it right now but post back if you have any problems or questions.
It could be modified to handle any data type if the comparison operator "==" was overloaded to handle the comparison. For example if the data type was a struct or class your would need to define "operator==" as one of the methods.

I guess I could do that, but it means I'll have something like 4 or 5 identical function bodies, with the exception of a single line. I was hoping I could avoid that seeing as duplicate code is such a bad thing :)
Besides, plenty of other functions use void pointers and somehow cast them to be used as they see fit.
Thanks.

Notes:
1. For templates the keywords typename and class are interchangable here.2. If the types are different enough then C++ can figure out what types you are using. Note that the types "int" and "unsigned char" were close enough that I had to specify which one to use.
For example type not specified but implied:
find(doubArray, doubItem, size)For example types had to be specified because C++
could not distiguish between them:
find<int>(intArray, intItem, size)
find<unsigned char>(ucharArray, ucharItem, size)Here is a sample program:
#include <iostream>
using namespace std;template<typename T>
int find(T array[], T item, int sizeofArray)
{
for (int i=0; i<sizeofArray; i++)
if (array[i] == item)
return i;
return -1;
};int main()
{
int intArray[] = {45, 34, 11, 8, 134}, intItem = 34;
double doubArray[] = {45.0, 34.0, 11.0, 8.0, 134.0}, doubItem = 8.0;
unsigned char ucharArray[] = {45, 34, 11, 8, 134}, ucharItem = 11;
int size = 5;
cout << "int find = " << find<int>(intArray, intItem, size) << endl;
cout << "doub find = " << find(doubArray, doubItem, size) << endl;
cout << "uchar find = " << find<unsigned char>(ucharArray, ucharItem, size) << endl;
}Output:
int find = 1
doub find = 3
uchar find = 2

Oh, I see you posted again. Thanks for that, though I had already got it.
You can see the fruits of my (thanks to your) labors at this question.
Thanks again. I'd never used templates in C++, but wow, are they beautiful! :)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |