Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign parts of struct to another struct in c

Tags:

c++

c

struct

I want something conceptually like this in c/c++

struct first{
   int a,b,c;
}my1;

struct second{
   int a,b,c;
   int extended;
}my2;

and somehow be able to have

my2=my1; 

(meaning only copy the same parts. leaving extended untouched)

I thought of solving it as

struct second{
     first first_;
     int extended;
 }my2;

and have

my2.first_ = my1;

but it is a bit ugly for me. is there any more clear solution for this? probably it is something like extending a structure or something?

like image 478
Roozbeh G Avatar asked Nov 26 '25 07:11

Roozbeh G


1 Answers

Like that:

struct second : first
{
    int extended;

    second& operator=(const first& f)
    {
        first::operator=(f); extended = 0; return *this;
    }
};
like image 162
iavr Avatar answered Nov 28 '25 22:11

iavr