C++ vs. Java inheritance rule for protected members

Consider the following Java program:

1
2
3
4
5
6
7
8
9
10
11
class Parent {
    protected int foo;
}

class Child1 extends Parent {}

class Child2 extends Parent {
    public Child2(Child1 other) {
        foo = other.foo;
    }
}


It can be rewritten in C++ like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Parent {
protected:
    int foo;
};

class Child1 : public Parent {};

class Child2 : public Parent {
public:
    Child2(Child1 &other) {
        foo = other.foo;
    }
};


However, the C++ version will not compile. g++ gives:

1
2
3
parent.cpp: In constructor ‘Child2::Child2(Child1&)’:
parent.cpp:3: error: ‘int Parent::foo’ is protected
parent.cpp:11: error: within this context


Obviously the inheritance rules of protected members are different. Can anyone suggest how I should rewrite the C++ version to match the Java program? Thanks.
Obviously the inheritance rules of protected members are different.


From what I understand, it's an error because Child2 isn't accessing its parent, it's accessing Child1's parent.

Regardless you probably shouldn't be doing this anyway, as it's illformed.

A better solution would be:

1
2
3
4
5
6
7
class Child2 : public Parent
{
public:
  Child2(const Child1& other)
    : Parent( other )  // copy construct 'Parent' based on other's Parent
  { }
};


The child class shouldn't be responsible for constructing the parent, as that's the parent's job.
Last edited on
closed account (1yR4jE8b)
it's an error because Child2 isn't accessing its parent, it's accessing Child1's parent.


That seems to be the case, the way Java seems to do that is pretty stupid IMO, that's not what protected members are for. That seems more akin to friend classes tbqh.

EDIT: According to the Java spec, the protected keyword gives package wide access to any class in that package. If you create another class outside of that package (even one that extends class Parent), access is denied.

This will compile just fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Stuff
{
    public static class Parent
    {
        protected int foo;
    }

    public static class Child1 extends Parent{}

    public static class Child2 extends Parent {
        public int function()
        {
            Child1 test = new Child1();
            test.foo = 99;

            return test.foo;
        }
    }

    public static class Child3
    {
        public int function()
        {
           Child1 test = new Child1();
           test.foo = 100;

           return test.foo;
        }
    }
}


Notice that Child3 doesn't extend Parent, but can still access protected members.

1. This completely bastardizes the protected keyword, this is not what it should be used for.
2. This is why I think the idea of packages in languages like Java and Ada is stupid.
Last edited on
closed account (1yR4jE8b)
To answer your question about making your C++ program equivalent, you would need to make Child2 a friend of Child 1.

But Disch's solution is better.
Last edited on
Topic archived. No new replies allowed.