Naming variables nested structures

closed account (jLNv0pDG)
1
2
3
4
5
6
7
8
9
10
11
12
13
struct child {
int age;
} alice = {1};

struct husband {
int age;
child child;
} bob = {10, alice};

struct wife {
float age;
child child;
} carol = {10.1, ann};


Few questions about naming variables:

1. Is it "ok" to use "age" for several different variables -- so I can go alice.age and carol.age -- or should I really be more specific and using int child_age and float wife_age?

2. On line 7 and 12 I use "child child" to keep things easy for myself so when I'm referencing I can just go "bob.child.age" -- is this ok? better suggestions?
Last edited on
If you are using C++ you should go for inheritance and write proper constructors
eg:
1
2
3
4
5
6
7
8
9
10
11
12
struct person
{
    int age;
    explicit person ( int age ) : age ( age ) {}
};

struct wife : public person
{
   // age inherited
   person child; // you may want this to be a pointer to child
   wife ( int age, const person &child ) : person ( age ), child ( child ) {}
};

closed account (jLNv0pDG)
I haven't done constructors/inheritance yet but it's good to know a better way of doing this exists.
I'd go with a container of children objects...

Topic archived. No new replies allowed.