C++中模板函数的使用

2014-11-24 07:22:15 · 作者: · 浏览: 0
#include

struct node
{
int a;
int b;
char c;
};


template
void myfun(T *pt , unsigned char *buf)
{
printf("sizeof(T):%d\n", sizeof(T));
*pt = *((T*)buf);
};


int main()
{
struct node info = {1,2,'a'};
struct node copyinfo = {0};
unsigned char *p = (unsigned char *)&info;

myfun(©info, p);

printf("%d %d %c\n", copyinfo.a, copyinfo.b, copyinfo.c);


return 0;
}