C++模板

来源:互联网 发布:ubuntu xrandr 编辑:程序博客网 时间:2024/06/05 00:25

非常简短的类模板。可能不常写,但要能看懂。


#include <iostream>

template <typename T> class A

{

public:

T b;

void fun()

{

std::cerr << b << std::endl;

}

};


int main()

{

A<int> a;

a.b = 5;

a.fun();

return 0;

}


输出“5”


"T"可以是“int”,也可以是“float”“double”等等。

0 0