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

📄 staticpoly.cpp

📁 C++ Templates 完全导引范例
💻 CPP
字号:
/* The following code example is taken from the book * "C++ Templates - The Complete Guide" * by David Vandevoorde and Nicolai M. Josuttis, Addison-Wesley, 2002 * * (C) Copyright David Vandevoorde and Nicolai M. Josuttis 2002. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */#include "statichier.hpp"#include <vector>// draw any GeoObjtemplate <typename GeoObj>void myDraw (GeoObj const& obj){    obj.draw();    // call draw() according to type of object}// process distance of center of gravity between two GeoObjstemplate <typename GeoObj1, typename GeoObj2>Coord distance (GeoObj1 const& x1, GeoObj2 const& x2){    Coord c = x1.center_of_gravity() - x2.center_of_gravity();    return c.abs();  // return coordinates as absolute values}// draw homogeneous collection of GeoObjstemplate <typename GeoObj>void drawElems (std::vector<GeoObj> const& elems){    for (unsigned i=0; i<elems.size(); ++i) {        elems[i].draw();    // call draw() according to type of element    }}int main(){    Line l;    Circle c, c1, c2;    myDraw(l);        // myDraw<Line>(GeoObj&) => Line::draw()    myDraw(c);        // myDraw<Circle>(GeoObj&) => Circle::draw()    distance(c1,c2);  // distance<Circle,Circle>(GeoObj1&,GeoObj2&)    distance(l,c);    // distance<Line,Circle>(GeoObj1&,GeoObj2&)    // std::vector<GeoObj*> coll;    // ERROR: no heterogeneous                                   //      collection possible    std::vector<Line> coll;   // OK: homogeneous collection possible    coll.push_back(l);        // insert line    drawElems(coll);          // draw all lines}

⌨️ 快捷键说明

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