How to make a function that you can pass any type of array to??
-
I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven
-
I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven
Yes that would work, but once inside your function, you have to know what type of array has been passed. You could do something like this:
enum TYPES { CHAR, DOUBLE, INT, ...other types... }; void myFunc(void* data, int data_type) { switch (data_type) { case CHAR: char* carray = (char*)data; .... do stuff break; case DOUBLE: double* darray = (double*)data; ...etc. } ... }
You will probably also want to add a size parameter to myFunc as well, unless all your arrays will be a fixed size. --Dean
-
Yes that would work, but once inside your function, you have to know what type of array has been passed. You could do something like this:
enum TYPES { CHAR, DOUBLE, INT, ...other types... }; void myFunc(void* data, int data_type) { switch (data_type) { case CHAR: char* carray = (char*)data; .... do stuff break; case DOUBLE: double* darray = (double*)data; ...etc. } ... }
You will probably also want to add a size parameter to myFunc as well, unless all your arrays will be a fixed size. --Dean
thanks steven
-
I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven
void ptrs are dangerous as they aren't typesafe and are best avoided where possible. Have a look at Boost Any http://www.boost.org/doc/html/any.html[^] Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
-
I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven
What's wrong with templates?
template<class T> void MyFunction(T* start, T* end) { for(T* iter=start; iter <= end; iter++) { cout << *iter; } } char temp [100]; MyFunction(temp, &temp[99]);
The beauty of this approach is that it works with things that provide iterators such as std::vector, as well as standard arrays. Untested code, so YMMV. -- Ian Darling If I was any more loopy, I'd be infinite.
-
Yes that would work, but once inside your function, you have to know what type of array has been passed. You could do something like this:
enum TYPES { CHAR, DOUBLE, INT, ...other types... }; void myFunc(void* data, int data_type) { switch (data_type) { case CHAR: char* carray = (char*)data; .... do stuff break; case DOUBLE: double* darray = (double*)data; ...etc. } ... }
You will probably also want to add a size parameter to myFunc as well, unless all your arrays will be a fixed size. --Dean
Not being funny, or mean to be offensive, but that's about the worst solution I could imagine in C++. * it requires massive amounts of work if you wish to add a new type. * it's still not typesafe - you can't validate what's passed in * and there are far far better approaches in C++ (eg, templates) -- Ian Darling If I was any more loopy, I'd be infinite.
-
Not being funny, or mean to be offensive, but that's about the worst solution I could imagine in C++. * it requires massive amounts of work if you wish to add a new type. * it's still not typesafe - you can't validate what's passed in * and there are far far better approaches in C++ (eg, templates) -- Ian Darling If I was any more loopy, I'd be infinite.
Yes, I agree -- it's quick, dirty, ugly... I get the feeling though that going into something like templates would not have been understood if the above could not have been done without help from this board (no offense meant to anyone). I try to approach these things from the standpoint that a less elegant but easier to understand solution might better serve beginners. Then, at a later time, the better way to do something can be learned when the supporting knowledge is available. A fine line has to be drawn however, as the easy code might be a terrible way to do things. I suppose I should have added a disclaimer saying that there are better ways of doing things. With that said, I hadn't even thought about templates at the time :-O --Dean
-
I think my last post wasn't understandable. I am trying to create a function that can accept any type of an array as a paramater. char[] or double[] or int[] etc. wouldn't it be myFunction(void *data){ do stuff } char tmp[100]; myFunction(tmp); I hope this makes sense. Steven
[EDIT] Sorry did not read all the posts, but this problem screams for a template approach... [/EDIT]
template<class T>myFunction(T* data)
{
// do stuff
}int main()
{
char tmp[100];
float fTmp[1000];
myFunction<char>(tmp);
myFunction<float>(fTmp);
}John
-
What's wrong with templates?
template<class T> void MyFunction(T* start, T* end) { for(T* iter=start; iter <= end; iter++) { cout << *iter; } } char temp [100]; MyFunction(temp, &temp[99]);
The beauty of this approach is that it works with things that provide iterators such as std::vector, as well as standard arrays. Untested code, so YMMV. -- Ian Darling If I was any more loopy, I'd be infinite.
template <typename ForwardIter> void MyFunction(ForwardIter start, ForwardIter end) { while(start != end) { doSomething(*start); ++start; } } char temp[100]; MyFunction(temp, temp + 100);
Would be a cleaner solution, as your will only work with
std::vector
. This'll work with any iterator. -- Gnnnnmmmpppppppfffffhhh! -
template <typename ForwardIter> void MyFunction(ForwardIter start, ForwardIter end) { while(start != end) { doSomething(*start); ++start; } } char temp[100]; MyFunction(temp, temp + 100);
Would be a cleaner solution, as your will only work with
std::vector
. This'll work with any iterator. -- Gnnnnmmmpppppppfffffhhh!Like I said, untested code, and I'm a little rusty on C++ templates, but I figured it was essentially ok, and certainly illustrated what I wanted to suggest. -- Ian Darling If I was any more loopy, I'd be infinite.