简体   繁体   中英

Getters and Setters with pointers

Okay so I'm a fairly new with C++ and I'm doing an assignment right now that requires many different classes. Now I'm experienced with Java and i may still have that programming mentality in my brain but my question is:

When you have two different classes, A and B , if B wants to access variables within A , I want to create a getter for the variable in A , but if that variable is a container that is heavy like a std::map , my question is would should i be making a getter that returns the pointer to the variable in A to access it in B ?

For containers like a map, there is no objection if a getter returns a handle to access the object in place. It could be a pointer, but a reference might be more C++-ish.

For objects that are compositions - objects that "own" other objects - getters should normally not provide direct access. In Java you should not return the reference, and in C++ you should not return a pointer or reference. This, however, is more a style issue, following the advice of the OO pundits.

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