私人班级数据

use*_*241 0 c++ stl

你应该保留除了班级功能以外的所有数据吗?例如:我有一个std ::整数列表,我需要在其他类中访问.你会如何迭代它,你真的想把它保密吗?

编辑:

我正在寻找对其他课程中每个元素的个人访问权限.

Las*_*loG 5

The real question is why you need to iterate over the list in the 'other class'. If you need to perform a specific operation in the client class you could have other choices:

  1. If you need to perform a well-defined operation (say, computing an average of the values in the list) then you can implement this functionality as a member function of the class that keeps the list.

  2. If you need to perform all kinds of operations on the list then you can build a generic iterator interface, which accepts functions or functors that implement the various operations and return whatever results you need.

Neither of these options require you to expose the list itself.