1

Talking about structs, is it possible to just copy a struct's fields to another struct without explicitly calling the type field name?

Let me show you an example:

struct StructA
{
   char Name[20];
   int Age;
};

struct StructB
{
   StructA FieldStructA;

   int SomeOtherDeclarations;
   // ...
};

So, I could access the StructA fields on StructB doing this:

StructB strB;
strB.FieldStructA.Name[0] = 0;

What I want to do is access the StructA fields through StructB without accessing the data field on StructB. Something like inheritance between classes. Like this:

StructB strB;
strB.Name[0] = 0;

I want to inherit StructA fields on StructB. I know that I could do that with classes, but I have to use struct for some reasons (interop, specific use of stack and so on).

Thanks for your time!

6
  • 1
    In C++ classes and structs are the same thing except for the default visibility of fields. Everything you can do with a class, you can also do with a struct. Commented Aug 15, 2012 at 19:47
  • If you settle on C++ there is inheritance for structs, just like for classes. struct B : A { }; works perfectly well. Commented Aug 15, 2012 at 19:51
  • 1
    Your answer is in the question title. If you are talking about C++. You should pick one of the two languages, really. Commented Aug 15, 2012 at 19:51
  • @Sylence: Another difference is that the inheritance of structs is public by default (it's private for classes). Commented Aug 15, 2012 at 20:03
  • How about writing C++ instead of C, i.e., std::string name;. Even in C I would probably use a dynamically allocated chunk of memory instead of a fixed size array. How would you possibly account for Mr. Frued Von Bissmark III? Commented Aug 15, 2012 at 20:06

4 Answers 4

5

You can do this in C++ using inheritance:

struct StructB: public StructA
{
   int SomeOtherDeclarations;
   // ...
};

Given that, you can then do

StructB strB;
strB.Name[0] = 0;
Sign up to request clarification or add additional context in comments.

4 Comments

For what it's worth, the default inheritance for structs is public already - so you could omit that if you like.
I'd prefer always being explicit about it. It'll just end up confusing people otherwise.
I think this is the best answer for C++ and mine is the best answer for C. Too bad the OP asked two questions in one.
The problem of doing this is that with inheritance I can only set the base struct fields starting at offset 0. What if I have to put those fields at offset 200h for example? Thanks btw for the reply.
1

If you are using gcc, try making it an anonymous member:

http://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html

struct StructA
{
  char Name[20];
  int Age;
};

struct StructB
{
   struct StructA;

   int SomeOtherDeclarations;
   // ...
};

1 Comment

There is no need for GCC. C11 officially has that feature. :)
1

In plain C, you have 2 choices as I see it:

1 - define those fields with a macro. Then you can use them in two structs

#define PERSON_FIELDS \
char Name[20]; \
int Age; \

struct StructA
{
STRUCT_A_FIELDS
};

struct StructB
{
   STRUCT_A_FIELDS

   int thing;
   // ...
};

and you can then use

StructB strB;
strB.Name[0] = 0;


StructA strA;
strA.Name[0] = 0;

In this case, StructA and StructB have no connection to each other. But you have the same fields. This gives you at least a bit of modularisation. Macros like this are commonly used to achieve/fake things that newer languages give you (such as inheritance or genericity).

2 - Alternatively you could keep your above code but use accessor functions to get the values:

struct StructA
{
   char Name[20];
   int Age;
};

struct StructB
{
   StructA FieldStructA;

   int thing;
   // ...
};


int StructB_Thing(struct Struct *obj)
{
    return obj->thing;
}

int StructB_Age(struct Struct *obj)
{
    return obj->FieldStructA.Age;
}

Comments

0

In C++, inheritance is used for interface re-use, not implementation reuse. You can easily get the latter C, but the former requires being aware of a little trick:

struct StructA
{
   char Name[20];
   int Age;
};

struct StructB
{
   StructA FieldStructA;

   int SomeOtherDeclarations;
   // ...
};

The trick is to understand that you can do

void setAge( struct StructA *s, int age ) {
    s->Age = age;
}

struct StructB b;
setAge( (struct StructA *)&b, 33 );
/* b.FieldStructA.Age is now 33 */

Note how you don't have to mention FieldStructA anywhere here: the code works because the 'base' fields are actually the first members of the 'derived' struct. So a value of type struct StructB * can be used in places where you access fields via a struct StructA *. This is the interface reuse: the same set of functions used for accessing StructA can be used for accessing the same fields in a StructB.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.