I understand the very basics of the public
and private
keywords (I'm still not sure what protected
does)
I have a question regarding "protection of data members for thread safety."
For example, say I have a class MyClass
that extends Thread and belonging to this class is a private data member called MyDataMember
. Suppose there are public accessor and setter functions for this data member which make sure to synchronize the data member.
Now this is all fine from an "external" point of view in that other threads can only set and retrieve the data member when synchronized properly. However, what about other functions within MyClass
? Say I have another function within MyClass
called DoSomething
. I know I should not attempt to access MyDataMember
directly, but I might forget from time to time to call the accessor/setter methods and access/set the data member directly.
My question is: Is there a keyword I can use to declare function members exclusive access to certain data members? This way, if I "accidentally" directly access a data member from a method that does not have exclusive access, then an error would occur. I just think this would make things a bit safer during development (at least for me!)