Sunday, July 12, 2009

C++ question?

Template functions





1 share one copy of the function template.





2are instantiated by the compiler.





3 have a maximum allowed number of type parameters.





4 are not more concise than having separate overloaded functions.





your answer please and what do you think about my dress

C++ question?
The answer is 1. The fact that you care what someone you've never met thinks about your dress pretty much proves why it is you're baffled by such a simple question: You aren't focused on anything important or useful, and probably ought to read your textbooks more closely, rather than Cosmo.
Reply:Of course you do. Problem is, you're never gonna get it. Report It

Reply:Note: A generic function and a template function are the same things.





Is this question supposed to read, "Which of the following is False?" If not, then perhaps I think too much. Go figure!


______________________________________...





1) share one copy of the function template. (TRUE)





In the example below, the compiler will replace the generic (or template) function with three specific functions: one that returns a float value, one that returns an int value, and one that returns a long value.





template%26lt;class T%26gt; T compare-values(T a, T b)


{


return ((a %26gt; b) ? a: b);


}





float compare_values(float a, float b);


int compare_values(int a, int b);


long compare_values(long a, long b);


______________________________________...





2) are instantiated by the compiler. (TRUE)





The template does not create any functions. Instead, it provides the compiler with directions about how to define a function. The compiler generates the appropriate function


______________________________________...





3) have a maximum allowed number of type parameters. (False)





You can declare functions that support multiple types. Your generic functions can support an unlimited number of generic types. The generic declaration form for generic functions that support multiple types is shown here:





template %26lt;class Ttype1, class Ttype2, ... class TtypeN%26gt;


return-type function-name ([parameter list])


{


// function body


}





When you declare a generic fucntion that supports multiple types, you should avoid declaring too many generic types within the function, because declaring too many types may create more confusion than solutions. You must also be sure that your function can always derive its return-type from the generic types the function receives as parameters.


______________________________________...





4) are not more concise than having separate overloaded functions. (TRUE?)





It is true that the programmer does not have to write separate versions of essentially the same function. He/she creates a single template, and includes function prototypes for the allowable function versions. However, function templates don't make your executable programs any shorter. You still end up with two separate function definitions, just as if you had defined each one manually.





Incidently, the final code doesn't contain any templates; it just contains the actual functions generated for your program. The benefits of templates is that they make generating multiple functions definitions simpler and more reliable—because the compiler automatically performs the task.





______________________________________...


No comments:

Post a Comment