Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Consider a c++ template funtion
template<class T>
T& Add(T a, T b){return a+b ;}

if this function is called as
T c = Add("SAM", "SUNG");

what will happen? What is the problem in the template
declaration/ How to solve the problem.

Answer Posted / skc

The error is adding Two pointers isn't alowed in C/C++.
The compiler imlicitly treats "SAM" as const char*. We need
to write a function with "explicit" keyword declaration like

explicit char * Add (const char* x1, const char* x2)
{
// check for null pointers.
// allocate strlen(x1)+strlen(x2)+1 using malloc
// say char*a1 = malloc...;
// check if malloc returns null..take corrective actions

// strcpy (a1, x1);
// strcat (a1, x2);
//strcat (a1, '\0');

return a1; // ask the caller to free the memory allocated
// for a1

}

since this function is writen explicit the compiler will
invoke this function and not call the default template
function.
this is what appears to me. haven't coded and verified.

// Regards, SADIQ

Is This Answer Correct ?    4 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to defines the function in c++?

1048


In which situation the program terminates before reaching the breakpoint set by the user at the beginning of the mainq method?

1015


How long does it take to get good at leetcode?

1116


Write a C++ Program to check whether a number is prime number or not?

1056


Do class declarations end with a semicolon?

1025


What is the difference between mutex and binary semaphore?

1112


How static variables and local variablesare similar and dissimilar?

993


Can I learn c++ without knowing c?

999


Differences between private, protected and public and give examples.

997


How can you quickly find the number of elements stored in a dynamic array?

984


What is nested class in c++?

893


What is the use of seekg in c++?

991


What is the best c++ compiler?

1052


If there are two catch statements, one for base and one for derived, which should come first?

986


Write a Program for find and replace a character in a string.

1013