将vector作为私人/公共成员进行类设计? [英] Class design with vector as a private/public member?

查看:143
本文介绍了将vector作为私人/公共成员进行类设计?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将容器类或某个其他类作为私有或公共成员放入类中的最佳方法是什么?



要求:



1.Vector<

2。需要添加和计数vector接口

解决方案

如果容器的状态是类的不变式的一部分,则在可能的情况下,它应该是私有的。



例如,如果容器代表三个维向量,则不变量的一部分可能是它总是精确地包含3个数字。将其公开显示为公开成员,将允许类外部的代码更改容器的大小,这反过来可能对需要容器大小恒定的任何例程产生问题。保持容器为私有状态限制了您的软件中可以将容器的大小更改为班级成员函数的位置。


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

解决方案

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.

这篇关于将vector作为私人/公共成员进行类设计?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆