| | |
Templates and return types
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
OK, I've been looking at templates recently and eveything was fine...never had a problem with them at any point. But then when I decided to write a function that would either return the given template type or not. the following code explains what I mean. I hope that makes sense all thanks
Chris
C++ Syntax (Toggle Plain Text)
template <class T> T find(char x, T array[]){ string str = "hello"; for(int i = 0; i < str.length(); i++){ if(x == str[i]) return array[i]; } // if it's not I need to return something...but what? }
Chris
Knowledge is power -- But experience is everything
I had already though of that, but what if T is a string? Since T could be absolutely anything I don't know what to do, it could be a class..
class...hmm maybe I should just return the default constructor for T?
T();
Chris
class...hmm maybe I should just return the default constructor for T?
T();
Chris
Last edited by Freaky_Chris; Jan 11th, 2009 at 1:05 pm.
Knowledge is power -- But experience is everything
•
•
Join Date: Mar 2008
Posts: 1,405
Reputation:
Solved Threads: 114
>maybe I should just return the default constructor for T?
You could do that, and add a parameter for the caller to check if anything was found, like this:
You could do that, and add a parameter for the caller to check if anything was found, like this:
C++ Syntax (Toggle Plain Text)
template <class T> T find(char x, T array[], bool &found){ string str = "hello"; found = true; for(int i = 0; i < str.length(); i++){ if(x == str[i]) return array[i]; } found = false; // Return default constructor }
Yer I think i'm going to go with something like that. Although it will be slightly different i'll just have a variable within my class that marks if it was found or not. Since that was only an example i'm actually overloading the operator[] so passing another parameter is kind out of the question lol.
Thanks for the input
Chris
Thanks for the input
Chris
Knowledge is power -- But experience is everything
![]() |
Similar Threads
- Modeling a frequently changing protocol (C++)
- Is a variant return type possible in C++? (C++)
- functions with variable number of arguments (C++)
- Templates questions (C++)
- Function templates and argument passing (C++)
- please check my code and help me (C++)
- C++ Templates (C++)
Other Threads in the C++ Forum
- Previous Thread: Disapearing objects!
- Next Thread: Creating a dynamic 2D array
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






