So here is what I know:
- It is wise not to expose your ivars directly in your API; rather use accessors
- A
const pointer to an object is not const just means that you can change the object but not redirect to where the pointer points
Here is my situation:
I have several related classes. I want to create a simple class that, through composition, combines them into one logical interface. Each of my private classes already has a public and private difference in its API, so I do not mind exposing them directly to users of my parent class. This means that it would be superfluous for me to write accessors for these Ivars, since classes already control what is open and what is not. But I do not want users to change the actual objects nested in this composite parent class.
So the only way I can do this is to use const pointers for these objects:
class Parent{ public: EnclosedClass1*const ec1;
Thus, there is no harm in the fact that someone can interact directly with the ec1 and ec2 APIs, but I want to make sure that this is a fairly reliable solution, since at least this person cannot change the actual resource, therefore, const pointers.
Does this make sense, is it good to use const pointers?
Alternatively, I could make them private completely, forget the pointers (this class manages these objects anyway), and just do the extra work of writing accessories for the public functions contained in these objects. But that just seems redundant, right?
source share