creating template function

kaustubh's Avatar, Join Date: Aug 2007
Go4Expert Member
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 )
then
concatnate(a,b)
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Are you saying it will not work or asking if it will work or not?
0
kaustubh's Avatar, Join Date: Aug 2007
Go4Expert Member
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 .
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Google for "partial specialization".
0
kaustubh's Avatar, Join Date: Aug 2007
Go4Expert Member
dawei,
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by kaustubh
dawei,
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.
strcpy.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
And strcat.
0
kaustubh's Avatar, Join Date: Aug 2007
Go4Expert Member
hello dawei,
there is a problem the defination of strcat is
Code:
strcat(int * a, int *b)
it will only take inside pointers ,so if you want it to work with add
it will be

Code:
 
#include<iostream>
#include<cstring>

char *add(int *a, int *b)
{
strcat(a,b);
return *a;
}

int main()
{
char a[] = "hi";
char b[] = "world";
cout<<endl<<add(a,b);
return 0;
}

OUTPUT:
hiworld 

but with error and terminated
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
Code:
#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:
Quote:
Originally Posted by Output
Hello
Bubba
Hello, Bubba

Hello, Bubba
0
kaustubh's Avatar, Join Date: Aug 2007
Go4Expert Member
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
.