I my codebase I never have more than a struct containing a struct (1 level). I don't have a rule for that, it's what emerged of my coding style. That makes it easy to know what's inside a given struct (only look at 1 struct definition).
As I use only C, I don't use operator overloading, function overloading, copy constructor… and it's not a problem for me. I prefer that there is no ambiguity in the code: if the "=" operator can mean "copy" or "deep copy" and you have no way of knowing without looking at the source, than I don't thing it's a good idea. Just the fact that it's possible to overload an operator make all C++ code ambiguous (for me).
If the '=' operator does a deep copy, you may need to write a function to do a shallow copy (if you need that at some point). So you would need to write two functions to have the same result as writing one.
I don't think that having to know what's inside a struct is a problem either. If you want to operate on some data, you know the data is in the struct. If you don't want to operate on the data, then you don't want it to be allocated and copied for no reason.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | typedef struct dyn_array_t {…} dyn_array_t;
void dyn_array_initialize( dyn_array_t* array, … ) {…}
void dyn_array_copy( dyn_array_t* from, dyn_array_t* to ) {…}
typedef struct thing_t {
dyn_array_t array;
…
} thing_t;
void thing_initialize( thing_t* thing ) {
dyn_array_initialize( &thing->array, … );
…
}
Thing a, b;
thing_initialize( &a );
b = a;
dyn_array_copy( &a->array, &b->array );
|