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)
|
Go4Expert Member
|
|
| 13Aug2007,13:33 | #1 |
|
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)
|
|
Go4Expert Founder
|
![]() |
| 13Aug2007,14:50 | #2 |
|
Are you saying it will not work or asking if it will work or not?
|
|
Go4Expert Member
|
|
| 13Aug2007,15:45 | #3 |
|
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 . |
|
Team Leader
|
![]() |
| 13Aug2007,16:35 | #4 |
|
Google for "partial specialization".
|
|
Go4Expert Member
|
|
| 13Aug2007,17:15 | #5 |
|
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. |
|
Go4Expert Founder
|
![]() |
| 13Aug2007,17:37 | #6 |
|
Quote:
Originally Posted by kaustubh |
|
Team Leader
|
![]() |
| 13Aug2007,19:10 | #7 |
|
And strcat.
|
|
Go4Expert Member
|
|
| 13Aug2007,21:12 | #8 |
|
hello dawei,
there is a problem the defination of strcat is Code:
strcat(int * a, int *b) 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
|
|
Team Leader
|
![]() |
| 13Aug2007,22:11 | #9 |
|
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;
}
Quote:
Originally Posted by Output |
|
Go4Expert Member
|
|
| 13Aug2007,23:11 | #10 |
|
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 . |