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 / pranay

MISTAKE:
1.returning a temp object so it goes out of scope.
2.using + for c-type strings.
SOLUTION:
string s = Add("SAM","SUNG");

Is This Answer Correct ?    1 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

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

552


What is c++ try block?

581


Why c++ is called oop?

593


Mention the storage classes in c++.

647


What is abstract class in c++?

587






Difference between declaration and definition of a variable.

589


What is the difference between method overloading and method overriding in c++?

562


What is polymorphism in c++? Explain with an example?

607


Reverse the Linked List. Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL

335


Write a Program for read a line from file from location N1 to N2 using command line arguments. Eg:exe 10 20 a.c

800


What are the advantages of c++ over c?

584


What is time h in c++?

585


Why do we use double in c++?

598


How would you use qsort() function to sort an array of structures?

722


Mention the ways in which parameterized can be invoked. Give an example of each.

572