⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 the_5255.htm

📁 C++标准库 C++标准库 C++标准库 C++标准库
💻 HTM
字号:
<HTML><HEAD><TITLE>Chapter 17:  The Traits Parameter</TITLE></HEAD><BODY><A HREF="ug1.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="tip_9088.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="usi_7948.htm"><IMG SRC="images/next.gif"></A><BR><STRONG>Click on the banner to return to the user guide home page.</STRONG><H1>Chapter 17:  The Traits Parameter</H1><UL><A HREF="usi_7948.htm"><UL><LI>17.1 Using the Traits Technique</LI></UL></A></UL><P>Consider the following problem.  You have a matrix that must work for all types of numbers, but the behavior of the matrix depends, in at least some measure, on the type of <SAMP>number</SAMP>.  This means your matrix can't handle all numbers in the same way.</P><P>Except for the behavioral difference, it sounds like the perfect problem for a template.  But you can't hang extra information on the number type because it's often just a built-in type, so you can't use a single template.  The template will do the same thing for every number type, which is just what we can't do in this case.  You could specialize, but then you have to re-implement the entire matrix class for every type of number.  It may well be that most of the class is the same.  Worse yet, if you want to leave your interface open for use with some unknown future type, you're requiring that future user to reimplement the entire class as well. </P><P>What you really want is to put everything that doesn't change in one place, and repeatedly specify only the small part that does change with the type.  The technique for doing this is generally referred to as using a <I>traits</I> <I>parameter</I>.</P><HR><A HREF="tip_9088.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="usi_7948.htm"><IMG SRC="images/next.gif"></A><P>&copy;Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -