关于void *返回值为double的问题,编译失败

whiteyundd whiteyundd 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
class mytest {
public:
int getDataInt(){
return u.i;
};
void setDataInt(int _i){
u.i=_i;
};
double getDataDouble(){
return u.d;
};
private:
union {
int i;
double d;

}u;
};

void* getValueI(mytest *d){
return (void *) d->getDataInt();
}
void* getValueD(mytest *d){
return (void *) d->getDataDouble();
}

int main()
{
mytest a;
int ia;
a.setDataInt(10);
ia = (int )(long) (getValueI)(&a);
printf("%d\n",ia);
return 0;
}


编译报错:
in function:void* getValueD(mytest*)
error: invalid cast from type 'double' to type 'void*'
把该函数修改为
void* getValueD(mytest *d){
return (void *)& d->getDataDouble();
}

则报如下错误:
error: invalid lvalue in unary ‘&’

对于getValueI 即返回值为int时,同样的写法则没有错误,
这是为什么呢?????
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
whiteyundd
C++ 语言 65,214 成员 250,514 内容