Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   creating template function (http://www.go4expert.com/forums/creating-template-function-t5832/)

kaustubh 13Aug2007 13:33

creating template function
I created a template function called add
like this
template(class T)
T add(T a, T b)

return a + b;


now this will work for number types like int float, and string , but will not work for char .
how can i say if ( T is char ) or
(a is char) (b is char )

shabbir 13Aug2007 14:50

Re: creating template function
Are you saying it will not work or asking if it will work or not?

kaustubh 13Aug2007 15:45

Re: creating template function
It is not working for char, so i want to modify this template function when it is a type of char
then it will join(a,b) . Like add('h','i') should give hi .

DaWei 13Aug2007 16:35

Re: creating template function
Google for "partial specialization".

kaustubh 13Aug2007 17:15

Re: creating template function
thank you very very much. Can you please tell me what is the c or c++ function to join to char . For example in above example i want to join a and b char.

shabbir 13Aug2007 17:37

Re: creating template function

Originally Posted by kaustubh
thank you very very much. Can you please tell me what is the c or c++ function to join to char . For example in above example i want to join a and b char.


DaWei 13Aug2007 19:10

Re: creating template function
And strcat.

kaustubh 13Aug2007 21:12

Re: creating template function
hello dawei,
there is a problem the defination of strcat is

strcat(int * a, int *b)
it will only take inside pointers ,so if you want it to work with add
it will be



char *add(int *a, int *b)
return *a;

int main()
char a[] = "hi";
char b[] = "world";
return 0;


but with error and terminated

DaWei 13Aug2007 22:11

Re: creating template function
Son, you're in a world of hurt. You really need to get some decent documentation. The declaration of strcat is as follows:

char * strcat ( char * destination, const char * source );

You are also writing C code in your C++. Why the hell would you do that? Although the C string functions will work, they have been deprecated. Your compiler is free to quit supporting them at any time.

C++ has a string class. It does all that you need. It even overloads the '+' operator and uses it for concatenation. Further, it grows as needed, so you don't have to worry about overflow.

If you are just messing around in an attempt to learn, that's fine. Write your own string class. Overload the "=", "==", "+", and "[]" operators. You'll learn a lot.

Here is some functioning code that illustrates both approaches.

#include <iostream>
#include <string>
#include <cstring>

using std::cout;
using std::endl;
using std::string;

int main()
    // The following is actually C code. 
    // Why write C code in C++ ????

    char first [] = "Hello";  // In most modern systems, these will be write protected
    char second [] = "Bubba"; // In other words, you can't use them as a destination
    char destination [16];    // This must be large enough to hold the result

    strcpy (destination, first);
    strcat (destination, ", ");
    strcat (destination, second);

    // This is C++ code
    cout << first << "\n" << second << "\n" << destination << endl;

    string newDestination = first;
    newDestination += ", ";
    newDestination += second;
    cout << "\n" << newDestination << endl;
    return 0;

Here is the output:

Originally Posted by Output
Hello, Bubba

Hello, Bubba

kaustubh 13Aug2007 23:11

Re: creating template function
I understand now that it's better to use strings.
I will try to convert it string and then concatenate.
The problem i had is can we create something like char[] add(char a[],char b[]) or can
we convert string to char array . The reason i am asking is because i have old projects containing char arrays , i sometimes need to concatenate them.
Thank you very much for your help

All times are GMT +5.5. The time now is 19:34.