Inheritance - protected

I’m taking the C++ course. The screenshot is taken from a lesson that talks about inheritance.
Now, is access to a member of a protected area not prohibited from outside the class even if it is an instance of that class? Maybe it’s a typo?

1 Like

I think you are asking this in the wrong forum section.
This is about Blender Asking questions. Not the programming section.

Have fun!

sorry, i moved it

Not for this case as you are accesing it from a derived classes (subclasses) which is allowed. the class child derives from parent.

Members declared as public are accessible from any part of the program.

Members declared as protected are accessible within the class itself or by derived classes (subclasses)
But *NOT outside the class

Members declared as private are accessible only within the class itself. NOT even derived classes can modify or access this.

Hope this explains it a bit better.

Thanks for the reply.
But the variable (worldPos) that you try to access from outside of class (an object of child class named kid) is actually protected and as you rightly say this can be accessed from the class itself but cannot be accessed from outside. Different thing if we had something like:

   Vector2 kidPos = kid.getWorldPos 

being a method declared in public section of parent class

I think I see where the confusion lies, and admittedly this is probably an oversight in the slide.

RedM3tal is correct, the code as laid-out in the slide is technically incorrect and would lead to a compiler error. As you’d be trying to access worldPos from outside of the child class, it’d be more accurate to do this:

void SomeFuncInChild()
   Vector2 kidPos = worldPos;

Meanwhile, the more appropriate way to get to worldPos from outside would be to use the getWorldPos() function still.

Thanks for the reply
I also think it was an oversight. It seems that he wanted to demonstrate exactly the opposite given the presence of a getter method that is used for encapsulation. The same author in a different course, talking about access modifiers, had focused on a similar detail and had highlighted the error generated. If I noticed it, I owe it to his teachings and to that previous course. Truly top teacher.
thanks again :+1:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms