社区
C语言
帖子详情
如何将类型作为参数传递
jiongdy
2006-12-31 10:19:46
我想用C实现如下功能:
void test(类型)
{
根据传入的类型,定义对应类型的变量;
}
而且不采用swtich case 语句。
实际上就是想实现C++中模板的功能。
请问各位有何高见!
...全文
770
18
打赏
收藏
如何将类型作为参数传递
我想用C实现如下功能: void test(类型) { 根据传入的类型,定义对应类型的变量; } 而且不采用swtich case 语句。 实际上就是想实现C++中模板的功能。 请问各位有何高见!
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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是由编译器实现的
珍惜生命远离CPP
2006-12-31
打赏
举报
回复
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);
java
类型
作为
参数传递
的问题
java
类型
作为
参数传递
的问题基本
类型
对象
类型
在实际开发中,在抽取公共方法的时候,对于一些值传递并没有做深入的了解,导致碰到了问题,现在对于值传递进行总结。 基本
类型
对于基本
类型
,在Java语言提供了八种基本
类型
。六种数字
类型
(四个整数型,两个浮点型),一种字符
类型
,还有一种布尔型,同时还包括我们经常使用到的BigDecimal
类型
: 在方法调用时,实际参数把它的值传递给对应的形式参数,方法执行中形式参数值的改变不影响实际传入参数的值,这个时候是值传递。 public static void ma
C#
参数传递
(引用
类型
参数)
方法中参数的传递方式主要有和(ref,out),而参数有可以分为和,这里主要讲一讲引用
类型
参数的值/引用
参数传递
。对于一个引用
类型
对象,不管是将其作为值
参数传递
还是作为引用
参数传递
,我们都可以在方法成员内部修改它的成员。
说说基本
类型
和引用
类型
作为
参数传递
的区别
结论: 1、基本数据
类型
做为形式
参数传递
,对形式参数的改变不会影响到实际参数。 2、引用数据
类型
比如String、Integer做为形式
参数传递
和基本数据
类型
做为形式
参数传递
一样,对形式参数的改变不会影响到实际参数。 3、引用数据
类型
比如自定义类Student做为形式
参数传递
,对形式参数的改变会影响到实际参数。 代码示例: package cn.zxj.com; public class Student { private String name; private int ag
String
类型
作为
参数传递
// String
类型
作为
参数传递
,跟基本
类型
是一样的 String str = "你好"; test(str); System.out.println(str);//string字符串是个常量,一旦定义就不能被改变。 StringBuffer sb = new StringBuffer("hello"); tes...
java中String对象作为
参数传递
问题
问题 java中将对象作为
参数传递
究竟是值传递还是引用传递? 1、基本
类型
作为
参数传递
时,是传递值的拷贝,无论你怎么改变这个拷贝,原值是不会改变的。 2、对象作为
参数传递
时,是把对象在内存中的地址拷贝了一份传给了参数。 且看下面代码 首先我们有一个Student类,它有两个成员变量name和age package test_code; public class Student { String name; int age; public Student(String name,int
C语言
70,026
社区成员
243,244
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章