【发布时间】:2017-02-19 09:01:27
【问题描述】:
//data type 1
typedef struct t_A{
int mJ;
}A;
//data type 2
typedef struct t_B{
int mK;
}B;
//Function returning B object which is a rValue
B funcRetB(void)
{
B test;
test.mK = 9;
return test;
}
void main(void)
{
A a;
a = (A)funcRetB(); //How to typecast this without defining a variable of B?
}
以上代码报错 “错误 C2440:‘类型转换’:无法从‘B’转换为‘A’”。 是否可以在 C 中解决此错误?
【问题讨论】:
-
您能解释一下为什么要这样做吗?你希望达到什么目标?
-
古玩探索C
-
@VishvajeetT - 那么你的好奇心就会被一个简单的“你不能那样做”来满足。 C 不支持结构等价。阅读stackoverflow.com/questions/4486401/…
-
@StoryTeller 谢谢。另外,你能解释一下吗? B b = funcRetB();//Works B b = (B)funcRetB();// 错误 C2440: 'type cast' : 无法从 'B' 转换为 'B'。类型转换相同的数据类型(名称等效)如何产生错误?
-
你用什么编译?我无法复制ideone.com/trCWXZ
标签: c