template-demo.cpp
来自「压缩包里有教材<<C++模式设计-基于QT4开源跨平台开发框架>」· C++ 代码 · 共 29 行
CPP
29 行
#include "fraction.h"#include "complex.h"#include <iostream>//start id=powertemplate <class T> T power (T a, int exp) { T ans = a; while (--exp > 0) { ans *= a; } return (ans);}//end//start id=mainint main() { Complex z(3,4), z1; Fraction f(5,6), f1; int n(19); z1 = power(z,3); /* First instantiation: T is Complex. */ f1 = power(f,4); /* Second instantiation: T is Fraction. */ z1 = power<Complex>(n, 4); /* Supply an explicit template parameter if the actual argument is not "specific" enough. This results in a call to a function that was already instantiated. */ z1 = power(n,5); /* Which version gets called? */}//end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?