Templates and return types

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Templates and return types

 
0
  #1
Jan 11th, 2009
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.
  1. template <class T>
  2. T find(char x, T array[]){
  3. string str = "hello";
  4. for(int i = 0; i < str.length(); i++){
  5. if(x == str[i]) return array[i];
  6. }
  7. // if it's not I need to return something...but what?
  8. }
I hope that makes sense all thanks

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,405
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Templates and return types

 
0
  #2
Jan 11th, 2009
Maybe return an index or value that will never be used, for example, if T = int, return (-1 for example), and then the caller can do a check to see if a value was found, if not, -1 is returned.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Templates and return types

 
0
  #3
Jan 11th, 2009
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
Last edited by Freaky_Chris; Jan 11th, 2009 at 1:05 pm.
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,405
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 114
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Templates and return types

 
0
  #4
Jan 11th, 2009
>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:
  1. template <class T>
  2. T find(char x, T array[], bool &found){
  3. string str = "hello";
  4. found = true;
  5. for(int i = 0; i < str.length(); i++){
  6. if(x == str[i]) return array[i];
  7. }
  8. found = false;
  9. // Return default constructor
  10. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Templates and return types

 
0
  #5
Jan 11th, 2009
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
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC