C++ class template | Datastructure and algorithm

Pubblicato il: 07 novembre 2020
sul canale di: code house
74
3

An introduction to function and #class templates in C++.



In this course we’ll often want to write classes that can store any data type: not just containers of ints or containers of strings but containers that can store any type you might think of. Such data structures are called ADTs: abstract data types. In #C++, we can write abstract data types (and also functions) using a feature called templates. Let’s look at some examples where #templates could be useful.


Suppose that, for some reason, you wanted to write a function to add two values together. The most obvious implementation of such a function might look like this. So far, so good! Now, your team starts using the add function all over the codebase and grows to appreciate the elegance of the abstraction. However, folks working on other parts of the codebase that deal with floating-point values might now grow envious of this elegant simplicity. They are still stuck in the dark ages of writing x + y themselves! To accommodate their requests, you implement another such utility function. You see where this is going: the next thing you know, you’re spending all of your working hours writing and testing overloaded implementations of the add function. All of these overloaded functions are identical except for their types; wouldn’t it be nice if we didn’t have to write out all of these overloads? If we go even further, imagine that someone asks you to write a version of the add function that takes an object of class BankAccount, but that you don’t have access to that class' definition. How can we write — and compile — a function that uses an unknown type?


C++ templates are a mechanism for specifying what a function or a class ought to look like without knowing everything about that function or class. A template is a pattern that the compiler can use to instantiate instances of a function or a class. Templates are most commonly used to write many versions of the same function or class that can work with different types. In our example of the add function, we can write one template and then let the compiler instantiate all of the actual functions. Here, we have specified using the keyword typename that T is the name of a type that can be substituted in when the compiler instantiates one of the overloaded add functions. So, when we try to add two integers together, the compiler will automatically generate an add function for us with T set to int, giving us a function with the signature int add(int x, int y). If we try to add two double s together, we’ll automatically get a function called double add(double x, double y). If we try to add two matrices or two BankAccount values, the compiler will instantiate these functions as required. We can tell the compiler explicitly what type we want to use by putting a type name between angle brackets, just like we would with something like std::vector. However, a lot of the time with function templates we don’t really need to do this: the compiler can see what types we’re passing in as arguments and infer the correct type for T.


We can also write class templates, in which the type name gets used in method parameters, return values or object field types. This is exactly how things like std::vector are implemented in the standard template library, and in fact, now we can see why the standard template library is called the standard template library. Instantiating objects of our class templates looks just like instantiating objects of standard template library classes.

So that’s how we can define function and class templates and how we can instantiate them. There’s a lot more to learn about templates, but that’s enough to get started on some examples.

TUTORIAL VIDEOS

■ c++ Datastructure and algorithm playlist link👇
   • Datastructure and Algorithm  


In questa pagina del sito puoi guardare il video online C++ class template | Datastructure and algorithm della durata di ore minuti seconda in buona qualità , che l'utente ha caricato code house 07 novembre 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 74 volte e gli è piaciuto 3 spettatori. Buona visione!