【发布时间】:2018-01-15 10:29:01
【问题描述】:
我们知道我们只能将一个结构对象分配给另一个具有相同类型的结构对象,但是为什么我们不能将一个 A 类型的结构对象分配给另一个 B 类型的结构对象呢?
这里,
struct typeA{
int inA1;
int inA2;
};
struct typeB{
int inB1;
int inB2;
};
int main(){
typeA varA;
typeB varB;
varA = varB//Here it will show compile time error. Why it can't
//assign object of typeB to object of typeA since both have
//same type of members
return 0;
}
为什么我们不能将 typeB 结构的对象分配给 typeA 结构的对象,因为它们的成员类型相同?
为什么不将不同类型的结构对象分配给另一个,因为它们可能具有相同的成员?
【问题讨论】:
-
"为什么不将不同类型的结构对象分配给另一个,因为它们可能具有相同的成员?" 您假设此行为将是默认行为,必须有一个规则来停止它的工作。实际上,必须有一个规则允许它工作。不同的类型是不同的,即使你认为它们是相同的。
标签: c++ object memory structure