使用向量作为私有/公共成员的类设计?
将容器类或其他类作为私有或公共成员放入类中的最佳方法是什么?
要求:
1.Vector< 某类> 在我的班级
2.Add 和向量计数需要接口
what is the best way to put a container class or a some other class inside a class as private or a public member?
Requirements:
1.Vector< someclass> inside my class
2.Add and count of vector is needed interface
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果容器的状态是类不变量的一部分,那么如果可能的话,它应该是私有的。
例如,如果容器表示三维向量,则不变量的一部分可能是它始终包含恰好 3 个数字。 将其公开为公共成员将允许类外部的代码更改容器大小,这反过来可能会导致任何需要容器大小保持不变的例程出现问题。 保持容器私有会限制软件中可以根据类的成员函数修改容器大小的位置。
If the container's state is part of the class's invariant, then it should, if possible, be private.
For example, if the container represents a three dimensional vector then part of the invariant might be that it always contains exactly 3 numbers. Exposing it as a public member would allow code external to the class to change the containers size, which in turn could cause problems for any routine which requires the container's size to be constant. Keeping the container private limits the places in your software where the container's size can be modified to the class's member functions.
成员是否被声明为私有或公共完全取决于您的应用程序。 你能提供更多细节吗?
声明成员时要记住的重要一点是,如果您提供“getter”来检索它,那么您就不再封装该对象。 相反,最好编写仅公开您希望公开的功能的包装器方法。
例如,对于 Vector 成员,您可以编写 AddItem 和 Clear 方法(如果这是您希望公开的所有功能)。
Whether a member is declared Private or Public depends entirely on your application. Could you give some more detail?
One important point to remember when declaring your member is that if you provide a "getter" to retrieve it, then you are no longer encapsulating that object. Instead, it can be good to write wrapper methods exposing only the functionality you wish to expose.
For example, with a Vector member, you might write an AddItem and Clear method, if that's all the functionality you wish to expose.
既然你谈论的是一个班级,我认为它应该是私人的。 如果您希望它是公共的,请创建一个结构 - 以明显表明您希望使用成员变量。
公开向量成员的一个可行的替代方法是创建访问者函数(或内部迭代器)。 这样您就可以更好地遵守 Demeter 定律:
从库导出例如
std::vector
时也要非常小心:客户端代码可能不会使用与您相同的 STL 实现确实如此,因此这些成员变量的布局可能会有所不同!Since you're talking about a class, I think it should be private. If you want it to be public, rather create a struct - to make it obvious that you want the members variables to be used.
A viable alternative to exposing the
vector
member is creating a visitor function (or an internal iterator). This way you obey the law of Demeter better:Also be very careful when exporting e.g. an
std::vector<T>
from a library, too: the client code might not use the same STL implementation as you did, so the layout of these member variables may differ!将所有成员设为私有并使用访问器方法,这允许您稍后更改实现。 只有在非常不寻常的情况下,我才会公开任何数据成员。
请记住,更改实现的情况比您想象的要频繁,这不仅仅是更改容器类型的情况,而且也许您想更改机制。 假设您将名称存储在列表中,一段时间后您可能会选择使用哈希来索引该列表,并且希望在每次添加新名称时更新哈希。 如果您的实现被适当封装,那么这很容易,如果您刚刚公开了向量,您将需要进行更改来调整接口(因此更改将产生连锁反应)。
如果这是新的,您可以阅读:http://en.wikipedia .org/wiki/Encapsulation_(classes_-_computers)
Make all members private and use accessor methods, this allows you to change the implementation later. Only in very unusual circumstances would I make any data member public.
Remember that chaning the implementation happens more often than you may imagine, its not just a case of changing the type of the container but maybe you want to change the mechanism. Say you were storing names in a list, after a while you may chose to index this list with a hash and would like to have the hash updated every time you add a new name. If your implementation is suitably encapsulated doing this is easy, if you have just exposed the vector you would need to make changes that will adjust the interface (and so the change will ripple out).
If this is new to new you have a read of: http://en.wikipedia.org/wiki/Encapsulation_(classes_-_computers)
还有第三种方法 - 有时最好从容器继承并重写它的方法来实现您的目标(例如线程安全)。 不管怎样,公开它几乎总是不是一个好主意。
There is a third way - sometimes it is better to inherit from the container and override it's methods to achieve your goal (for example thread safety). Anyway, making it public almost always isn't a good idea.
考虑到您想要将容器封装在另一个类中意味着它不能是公共的,并且您的类的公共方法不应该公开有关容器的任何特定于实现的内容。 这样,您的类(即容器)的实现就可以更改,而无需更改其接口。
Considering that you want to encapsulate the container inside another class implies that it cannot be public, and also the public methods of your class should not expose anything implementation-specific about the container. That way the implementation of your class (i.e. the container) can be changed without changing its interface.