首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

函数指针参数的类型转换,该如何解决

2013-06-26 
函数指针参数的类型转换typedef struct{int i} s1typedef struct{int i} s2void fuc1(void (*test)(in

函数指针参数的类型转换


typedef struct{
int i;
} s1;
typedef struct{
int i;
} s2;

void fuc1(void (*test)(int,s1))
{
/*do sth.*/
}
void fuc2(void (*test)(int,s2))
{
fuc1(test);
}

s1和s2是相同类型但名称不同的结构体,目的是为了隐藏s1

不能将参数 1 从“void (__cdecl *)(int,s2)”转换为“void (__cdecl *)(int,s1)

求破???
[解决办法]
看不懂你的目的。
[解决办法]
你都相同类型了还能隐藏什么?换个思路吧。
[解决办法]
union Test {
  void (*test_1)(int, s1);
  void (*test_2)(int, s2);
};

void fuc2(void (*test)(int,s2))
{
  Test t;
  t.test_2 = test;
  fuc1(t.test_1);
}
[解决办法]
需要自行做强制类型转换。

[解决办法]
引用:

typedef struct{
int i;
} s1;
typedef struct{
int i;
} s2;

void fuc1(void (*test)(int,s1))
{
/*do sth.*/
}
void fuc2(void (*test)(int,s2))
{
fuc1(test);
}

s1和s2是相同类型但名称不同的结构体,目的是为了隐藏s1

不能将参数 1 从“void (__cdecl *)(int,s2)”转换为“void (__cdecl *)(int,s1)

求破???


你搞错了,s1与s2并不是相同的类型,它们是具有相同类型成员子对象的不同类型,所以上述两种函数指针类型是不能互相隐式转换的。
[解决办法]
引用:
Quote: 引用:


typedef struct{
int i;
} s1;
typedef struct{
int i;
} s2;

void fuc1(void (*test)(int,s1))
{
/*do sth.*/
}
void fuc2(void (*test)(int,s2))
{
fuc1(test);
}

s1和s2是相同类型但名称不同的结构体,目的是为了隐藏s1

不能将参数 1 从“void (__cdecl *)(int,s2)”转换为“void (__cdecl *)(int,s1)

求破???


你搞错了,s1与s2并不是相同的类型,它们是具有相同类型成员子对象的不同类型,所以上述两种函数指针类型是不能互相隐式转换的。

++
[解决办法]
只能楼主设想太美好了
虽然s1,s2结构体组织是相同的,但是这不能说明s1,s2是相同类型的~

回调函数的参数不同(s1,s2)怎么可能编译通过呢~ 

热点排行