我想要在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;
}
};