[英]assign parts of struct to another struct in c
我想要在C / C ++中这样的概念
struct first{
int a,b,c;
}my1;
struct second{
int a,b,c;
int extended;
}my2;
并且以某种方式能够拥有
my2=my1;
(意味着仅复制相同的部分。保持扩展不变)
我认为解决它是
struct second{
first first_;
int extended;
}my2;
并有
my2.first_ = my1;
但这对我来说有点丑。 有没有更明确的解决方案? 大概是像扩展结构之类的东西?
像那样:
struct second : first
{
int extended;
second& operator=(const first& f)
{
first::operator=(f); extended = 0; return *this;
}
};
同样有点丑陋,但这是:
my1 = *(first*)&my2;
您可以将=
运算符重载为struct second
:
second & operator=(const first & s) {
this->a = s.a;
this->b = s.b;
this->c = s.c;
return *this;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.