Search code examples
c++classimplementationredefinition

Implementation of a class if a data member is redefined


What happens to the implementation of a class if we redefine a data member? for example, suppose we have:

class foo {
public:
    int a;
    char *b;
};
...
class bar : public foo {
public:
    float c;
    int b;
};

Does the representation of a bar object contain one b field or two? If two, are they both accessible, or only one? Under what circumstances?


Solution

  • It contains two, but one of them is called foo::b

    int main() {
        bar x;
        x.b = 0;    // access bar::b
        x.foo::b = 0;   // access foo::b
    }