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                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C++  >>  C++ General
 
 


 

 
 STL interview questions  STL Interview Questions
 OOPS interview questions  OOPS Interview Questions
 C++ General interview questions  C++ General Interview Questions
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  
 
  Re: 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
# 1
i will provide error.

declare template function as,
T Add(T a, T b){return a+b ;}
 
Is This Answer Correct ?    1 Yes 0 No
Prasad
 
  Re: 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
# 2
There are two things to be considered here.

First, T will be an unknown type in the place where we call 
which gives a compilation error.

Second, if we call like this:

std::string c = Add("SAM", "SUNG"); or,
char* c = Add("SAM", "SUNG"); 

the compiler will convey an error since the arguments to 
Add are interpretted as char pointers. The error may be 
something like, "cannot add two pointers....".
 
Is This Answer Correct ?    0 Yes 0 No
Mms Zubeir
 
 
 
  Re: 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
# 3
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 ?    0 Yes 0 No
Skc
 
  Re: 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
# 4
1) Compiler will throw the error as C++ doesnt support + 
operator for string. Another point is T c = Add
("SAM","SUNG"), where the function call assign to a 
TEMPLATE object type, it syntactical error.

2)We need to write a seperate add function as given below

char* Add( const char *s1, const char*s2);

This will solve the problem
 
Is This Answer Correct ?    0 Yes 0 No
Sv
 

 
 
 
Other C++ General Interview Questions
 
  Question Asked @ Answers
 
What is Object Oriented programming.what is the difference between C++ and C? Infosys5
Write a program that read 2o numbers in and array and output the second largest number. Can anybody help??  2
What are the basics of classifying different storage types, why? Symphony2
In a class only declaration of the function is there but defintion is not there then what is that function? Hughes4
what is difference between static and non-static variables  4
What are the differences between a struct in C and in C++? Wipro4
Is there any difference between dlearations int* x and int *x? If so tell me the difference? Lason13
The "virtual" specifier in a member function enables which one of the following? a) Monmorphism b) Late binding c) Metamorphism d) Solomorphism e) Inheritance Quark3
Implement strncpy  2
Write the program for fibonacci in c++?  3
What are the different types of Storage classes?  3
Explain the need for "Virtual Destructor"? Infosys1
What are the total number of lines written by you in C/C++? What is the most complicated or valuable program written in C/C++? Intel1
How do you link a C++ program to C functions?  2
How to stop conversions among objects? Symphony1
When copy constructor can be used? Symphony4
Implement strcmp Citadel2
class Alpha { public: char data[10000]; Alpha(); ~Alpha(); }; class Beta { public: Beta() { n = 0; } void FillData(Alpha a); private: int n; }; How do you make the above sample code more efficient? a) If possible, make the constructor for Beta private to reduce the overhead of public constructors. b) Change the return type in FillData to int to negate the implicit return conversion from "int" to "void". c) Make the destructor for Alpha virtual. d) Make the constructor for Alpha virtual. e) Pass a const reference to Alpha in FillData Quark1
How long does this loop run: for(int x=0; x=3; x++) a) Never b) Three times c) Forever Quark9
Find the second maximum in an array? HCL2
 
For more C++ General Interview Questions Click Here 
 
 
 
 
 
   
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