如何将类型作为参数传递

jiongdy 2006-12-31 10:19:46
我想用C实现如下功能:
void test(类型)
{
根据传入的类型,定义对应类型的变量;
}
而且不采用swtich case 语句。
实际上就是想实现C++中模板的功能。
请问各位有何高见!
...全文
624 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqpp 2007-01-26
  • 打赏
  • 举报
回复
typedef struct
{
int line;
int row;
void* content;
void (*op_f[xxx])( void* dest, void* src1, void* src2 );
}Matrix;

构造的时候同时初始化操作方法。
后来是用就用 matrix.op_f[OP_COPY](...) 后来是用就用 matrix.op_f[OP_ADD](...) ...
猫腻儿姐姐 2007-01-26
  • 打赏
  • 举报
回复
似乎只有模板技术中类型才可以作为参数!
doubhor 2007-01-25
  • 打赏
  • 举报
回复
C++中的模板行为发生在编译阶段,这时候编译器做了一个类似替换的行为,生成了一系列的函数。
C++中的虚函数也可以实现根据不同类型做不同处理的能力,是靠虚函数表实现的。
所以怎么实现关键是看你要达到什么目标,在不能改变编译器行为前提下,可以采用类似虚函数表的做法,在某一个地方设置一个函数指针,然后这个指针随不同类型而不同。
由于不知道你要实现什么功能,所以不好写出更加详细的代码。

完全模拟模板是非常困难,甚至在一定情况下是不可能的,能够达到自己的目的就可以了。
jiongdy 2007-01-04
  • 打赏
  • 举报
回复
高手都到哪去了?
netxuning 2007-01-04
  • 打赏
  • 举报
回复
学习...
jiongdy 2007-01-04
  • 打赏
  • 举报
回复
自己再顶一下
jiongdy 2007-01-03
  • 打赏
  • 举报
回复
楼上的方法实际上是统一按照int类型来处理数据的,
我现在改用memcpy的方式,没有什么问题了
Matrix MConstruct2(int line,int row,PrivateType type)
{
Matrix temp;
temp.line=line;
temp.row=row;
temp.type = type;
temp.content = malloc(line*row*type);
return temp;
}

Matrix MConstruct(Matrix A)
{
Matrix temp;
temp.line=A.line;
temp.row=A.row;
temp.type=A.type;
temp.content=malloc(A.line*A.row*A.type);
memcpy(temp.content,A.content,A.line*A.row*A.type);
return temp;
}
但是如果我要对矩阵里的数据进行+、-运算
又有困难了,还请大家多想想看
youjing243 2007-01-03
  • 打赏
  • 举报
回复
你可以定义一个枚举,不过需要使用switch,基本实现功能,不过在测试STRING的时候会出现戏剧性的结果,我也不清楚原因,呵
#include <stdio.h>
#include <stdlib.h>
typedef enum {CHAR=1,INT=4,DOUBLE=8,STRING=12}PrivateType;
typedef struct
{
void* content;
int line;
int row;
PrivateType type;
}Matrix;

Matrix MConstruct2(int line,int row,PrivateType type)
{
Matrix mat;
mat.line=line;
mat.row=row;
mat.type=type;
mat.content = malloc(line*row*type);
return mat;
}

//clone
Matrix MConstruct(Matrix A)
{
Matrix new_mat;
int i;
int *mat_ptr;
new_mat.line=A.line;
new_mat.row=A.row;
new_mat.type=A.type;
new_mat.content=malloc(A.line*A.row*A.type);
mat_ptr=(int *)new_mat.content;
for (i=0;i<A.line*A.row;i++)
mat_ptr[i]=*((int*)(A.content)+i);
return new_mat;
}
测试代码如下:
int main(int argc, char *argv[])
{
Matrix mat;
Matrix mat_clone;
int i;
char *tmp="youjing243";
PrivateType type=CHAR;
mat=MConstruct2(4,4,type);
for(i=0;i<mat.line*mat.row;i++)
{
switch(mat.type)
{
case INT:
*((int *)mat.content+i)=1;
break;
case CHAR:
*((char *)mat.content+i)='A';
break;
case DOUBLE:
*((double *)mat.content+i)=3.14;
break;
case STRING:
strcpy((char *)((char *)mat.content+i),tmp);
break;
}
}

for(i=0;i<mat.line*mat.row;i++)
{
switch(mat.type)
{
case INT:
printf("%d\t",*((int *)mat.content+i));
break;
case CHAR:
printf("%c\t",*((char *)mat.content+i));
break;
case DOUBLE:
printf("%d\t",*((double *)mat.content+i));
break;
case STRING:
printf("地址:%p\t",((char *)mat.content+i));
printf("值:%8s\n",(char *)((char *)mat.content+i));
break;
}
}
printf("\n行:%d,列:%d\n\n\n",mat.line,mat.row);
mat_clone=MConstruct(mat);
for(i=0;i<mat_clone.line*mat_clone.row;i++)
{
switch(mat_clone.type)
{
case INT:
printf("%d\t",*((int *)mat_clone.content+i));
break;
case CHAR:
printf("%c\t",*((char *)mat_clone.content+i));
break;
case DOUBLE:
printf("%d\t",*((double *)mat_clone.content+i));
break;
case STRING:
printf("地址:%p\t",((char *)mat_clone.content+i));
printf("值:%8s\n",(char *)((char *)mat_clone.content+i));
break;
}
}
printf("\n行:%d,列:%d",mat_clone.line,mat_clone.row);
getchar();
system("PAUSE");
return 0;
}
jiongdy 2007-01-02
  • 打赏
  • 举报
回复
关键是要把类型作为实参传到函数里啊

我贴一下简单的代码如下:
#define INT sizeof(int)
#define DOUBLE sizeof(double)

typedef struct
{
void* content;
int line;
int row;
int type;//这里不知道该怎么定义好?
}Matrix;

//构造对应类型的矩阵
Matrix MConstruct2(int line,int row,int type)
{
Matrix temp;

temp.line=line;
temp.row=row;
temp.uType = type;

temp.content = malloc(line*row*type);
return temp;
}

//用一个矩阵构造相同新的矩阵
Matrix MConstruct(Matrix A)
{
Matrix temp;
int i;
temp.line=A.line;
temp.row=A.row;
temp.type=A.type;

temp.content=malloc(A.line*A.row*A.type);

//现在要对temp1.content进行赋值,麻烦就来了,下面是按int类型赋值的,怎么改好呢?
temp1=(int *)temp.content;
for (i=0;i<A.line*A.row;i++)
temp1[i]=*((int*)(A.content)+i);

return temp;
}

不知道能否实现?
还请高手们帮想一想。
smileflyer 2007-01-02
  • 打赏
  • 举报
回复
宏是个办法!
cqpp 2006-12-31
  • 打赏
  • 举报
回复
只有用其他方法实现了你要求了!这条路是不行的
柯本 2006-12-31
  • 打赏
  • 举报
回复
即使能判断到类型,C也不能在运行时在程序中定义变量,C++的template是由编译器实现的
  • 打赏
  • 举报
回复
C语言估计做不到
晨星 2006-12-31
  • 打赏
  • 举报
回复
呵呵,他问的是C语言。
ouyh12345 2006-12-31
  • 打赏
  • 举报
回复
得模拟继承。
晨星 2006-12-31
  • 打赏
  • 举报
回复
估计做不到。
lauxp 2006-12-31
  • 打赏
  • 举报
回复
template不用,想在runtime搞?
目前唯一的办法:C++重新设计为动态语言
seas110 2006-12-31
  • 打赏
  • 举报
回复
#define Type(type){type temp;}


typedef int ch;
Type(ch);

69,335

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧