模板和泛型編程-------外文翻譯.doc
約17頁DOC格式手機(jī)打開展開
模板和泛型編程-------外文翻譯,templates and generic programminggeneric programming involves writing code in a way that is independent of any particular type. when we use a generic program we...
內(nèi)容介紹
此文檔由會(huì)員 wanli1988go 發(fā)布
Templates and Generic Programming
Generic programming involves writing code in a way that is independent of any particular type. When we use a generic program we supply the type(s) or value(s) on which that instance of the program will operate. The library containers, iterators, and algorithms described in Part II are examples of generic programming. There is a single definition of each container, such as vector, but we can define many different kinds of vectors that differ by the element type that the vector contains.Templates are the foundation of generic programming. We can, and have, used templates without understanding how they are defined. In this chapter we'll see how we can define our own template classes and functions.Generic programming, like object-oriented programming, relies on a form of polymorphism. The polymorphism in OOP applies at run time to classes related by inheritance. We can write code that uses such classes in ways that ignore the type differences among the base and derived classes. As long as we use references or pointers to the base type, we can use the same code on objects of the base type or a type derived from that type.Generic programming lets us write classes and functions that are polymorphic across unrelated types at compile time. A single class or function can be used to manipulate objects of a variety of types. The standard library containers, iterators, and algorithms are good examples of generic programming. The library defines each of the containers, iterators, and algorithms in a type-independent manner. We can use library classes and functions on most any kind of type. For example, we can define a vector of Sales_item objects even though the designers of vector could have had no knowledge of our application-specific class. In C++, templates are the foundation for generic programming. A template is a blueprint or formula for creating a class or a function. For example, the standard library defines a single class template that defines what it means to be a vector. That template is used to generate any number of type-specific vector classesfor example, vector or vector. Part II showed how to use generic types and functions; this chapter shows how we can define our own templates.
模板和泛型編程
所謂泛型編程就是以獨(dú)立于任何特定類型的方式編寫代碼。使用泛型程序時(shí),我們需要提供具體程序?qū)嵗僮鞯念愋突蛑?。第二部分中描述的?biāo)準(zhǔn)庫的容器、迭代器和算法都是泛型編程的例子。每種容器(如vector)都有單一的定義,但可以定義許多不同種類的vector,它們的區(qū)別在于所包含的元素類型。模板是泛型編程的基礎(chǔ)。使用模板時(shí)可以無須了解模板的定義。本章將介紹怎樣定義自己的模板類和模板函數(shù)。泛型編程與面向?qū)ο缶幊桃粯樱家蕾囉谀撤N形式的多態(tài)性。面向?qū)ο缶幊讨械亩鄳B(tài)性在運(yùn)行時(shí)應(yīng)用于存在繼承關(guān)系的類。我們能夠編寫使用這些類的代碼,忽略基類與派生類之間類型上的差異。只要使用基類的引用或指針,基類類型或派生類類型的對(duì)象就可以使用相同的代碼。在泛型編程中,我們所編寫的類和函數(shù)能夠多態(tài)地用于跨越編譯時(shí)不相關(guān)的類型。一個(gè)類或一個(gè)函數(shù)可以用來操縱多種類型的對(duì)象。標(biāo)準(zhǔn)庫中的容器、迭代器和算法是很好的泛型編程的例子。標(biāo)準(zhǔn)庫用獨(dú)立于類型的方式定義每個(gè)容器、迭代器和算法,因此幾乎可以在任意類型上使用標(biāo)準(zhǔn)庫的類和函數(shù)。例如,雖然vector的設(shè)計(jì)者不可能了解應(yīng)用程序特定的類,但我們能夠定義Sales_item對(duì)象組成的vector。在C++中,模板是泛型編程的基礎(chǔ)。模板是創(chuàng)建類或函數(shù)的藍(lán)圖或公式。例如,標(biāo)準(zhǔn)庫定義了一個(gè)類模板,該模板定義了vector的含義,它可以用于產(chǎn)生任意數(shù)量的特定類型的vector類,例如,vector或vector。本書第二部分介紹了怎樣使用泛型類型和泛型函數(shù),本章將介紹怎樣自定義模板。
Generic programming involves writing code in a way that is independent of any particular type. When we use a generic program we supply the type(s) or value(s) on which that instance of the program will operate. The library containers, iterators, and algorithms described in Part II are examples of generic programming. There is a single definition of each container, such as vector, but we can define many different kinds of vectors that differ by the element type that the vector contains.Templates are the foundation of generic programming. We can, and have, used templates without understanding how they are defined. In this chapter we'll see how we can define our own template classes and functions.Generic programming, like object-oriented programming, relies on a form of polymorphism. The polymorphism in OOP applies at run time to classes related by inheritance. We can write code that uses such classes in ways that ignore the type differences among the base and derived classes. As long as we use references or pointers to the base type, we can use the same code on objects of the base type or a type derived from that type.Generic programming lets us write classes and functions that are polymorphic across unrelated types at compile time. A single class or function can be used to manipulate objects of a variety of types. The standard library containers, iterators, and algorithms are good examples of generic programming. The library defines each of the containers, iterators, and algorithms in a type-independent manner. We can use library classes and functions on most any kind of type. For example, we can define a vector of Sales_item objects even though the designers of vector could have had no knowledge of our application-specific class. In C++, templates are the foundation for generic programming. A template is a blueprint or formula for creating a class or a function. For example, the standard library defines a single class template that defines what it means to be a vector. That template is used to generate any number of type-specific vector classesfor example, vector or vector. Part II showed how to use generic types and functions; this chapter shows how we can define our own templates.
模板和泛型編程
所謂泛型編程就是以獨(dú)立于任何特定類型的方式編寫代碼。使用泛型程序時(shí),我們需要提供具體程序?qū)嵗僮鞯念愋突蛑?。第二部分中描述的?biāo)準(zhǔn)庫的容器、迭代器和算法都是泛型編程的例子。每種容器(如vector)都有單一的定義,但可以定義許多不同種類的vector,它們的區(qū)別在于所包含的元素類型。模板是泛型編程的基礎(chǔ)。使用模板時(shí)可以無須了解模板的定義。本章將介紹怎樣定義自己的模板類和模板函數(shù)。泛型編程與面向?qū)ο缶幊桃粯樱家蕾囉谀撤N形式的多態(tài)性。面向?qū)ο缶幊讨械亩鄳B(tài)性在運(yùn)行時(shí)應(yīng)用于存在繼承關(guān)系的類。我們能夠編寫使用這些類的代碼,忽略基類與派生類之間類型上的差異。只要使用基類的引用或指針,基類類型或派生類類型的對(duì)象就可以使用相同的代碼。在泛型編程中,我們所編寫的類和函數(shù)能夠多態(tài)地用于跨越編譯時(shí)不相關(guān)的類型。一個(gè)類或一個(gè)函數(shù)可以用來操縱多種類型的對(duì)象。標(biāo)準(zhǔn)庫中的容器、迭代器和算法是很好的泛型編程的例子。標(biāo)準(zhǔn)庫用獨(dú)立于類型的方式定義每個(gè)容器、迭代器和算法,因此幾乎可以在任意類型上使用標(biāo)準(zhǔn)庫的類和函數(shù)。例如,雖然vector的設(shè)計(jì)者不可能了解應(yīng)用程序特定的類,但我們能夠定義Sales_item對(duì)象組成的vector。在C++中,模板是泛型編程的基礎(chǔ)。模板是創(chuàng)建類或函數(shù)的藍(lán)圖或公式。例如,標(biāo)準(zhǔn)庫定義了一個(gè)類模板,該模板定義了vector的含義,它可以用于產(chǎn)生任意數(shù)量的特定類型的vector類,例如,vector或vector。本書第二部分介紹了怎樣使用泛型類型和泛型函數(shù),本章將介紹怎樣自定義模板。