ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
Do you have a collection of Interview Questions and interested to share with us!!
Please send that collection to along with your userid / name. ThanQ
Google
 
Categories >> Software >> Programming-Languages >> C++ >> C++-General
 
 
 
Question
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.
 Question Submitted By :: Skc
I also faced this Question!!     Rank Answer Posted By  
 
Answer
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
 
0
Skc
 
View All Answers
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com