简体   繁体   中英

When I set protected data in a base class will it change the value of the protected data in the derived class?

I have a base class and a derived class.

In the base class I have a setvalue function and getvalue function.

I use the setvalue function to set the protected data which belongs to the base class.

Because the derived class could inheritance the protected data and getvalue function from base class.

My question is that if I use the setvalue function to set the protected data in the base class.

How about the value of the protected data in the derived class?

To answer this, we should firstly be clear that our targets are "objects" but not classes.

You have an object with the type "derived class", if change the value of this object through "setvalue(...)", it's true that the protected value or "getvalue()" function of this object will response this change.

Though you have two classes, when you create an object, it's just one object. The data members belong to the object, and there will be just one copy of the protected data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM