适当的方法以提供对类内部数组元素的访问
我开始在ADA(汽车)世界中重构已经太大且耦合的项目(SO C ++ 11宽容,但没有真正的动态分配,一个常见的数学库模仿了某些STL容器等...),我面临着一个反复出现的问题/审讯。
从索引甚至其他确定元素字段中,将被认为是“最佳”(或有效/相关)的方法,可以提供对类内部数组元素的访问权限。
例如,假设我有一个类,可以从我们的组件中与外部世界建立接口(因此,基本上是从项目的其他组件中获取一些数据,以纯C结构交换,固定尺寸等用于嵌入式约束),将其包装全部包装,为我们的组件算法提供其他信息和访问方法。
如果我的内部数组如下(矢量模拟需要max_size,但表现得像该外部界限内的向量,具有“当前大小”,push_back等,以及大多数常规的搭配器),
typedef custom_vector_like<element, MAX_ELEMENTS> data_array_type;
class ...
{
data_array_type m_data;
}
这被认为是一种有效的方法为组件算法行为提供安全的“获取数据”方法。
通常,我想对数组中的元素提供const和const Ref,但是如何处理意外的IDX(无界)?还是如何处理“查找验证X条件的元素”,如果没有找到?
您会使用参考参数并返回Bool以获得成功(但是用户代码需要定义要在getter调用中填充的元素对象...)? 还是只是断言并让用户(在更糟糕的情况下进行调试?)他发送了无义数据作为输入?
或者,也许仅提供对整个容器(const或not)的访问权,并让用户处理循环 /访问成员?
I am starting to work on refactoring an already too big and heavily coupled project, in ADAS (automotive) world (so C++ 11 tolerated, but no true dynamic allocation, a common math library mimics some of the stl containers etc...), and I'm facing a recurring issue/interrogation.
What would be considered "best" (or valid/relevant) approach to provide access to a class internal array's element, from index or even from other determining element's field.
For instance, let's say I have an class that makes the interface with outside world from our component (so basically taking some data from other components of the projects, exchanged as pure C structs, with fixed sizes etc for embedded constraints), wrapping it all, providing additional info and access method for our component algos.
If I have an internal array like the following (the vector mimic requires a MAX_SIZE, but behaves like a vector within this outer bound, with a "current size", push_back etc, and most usual accessers)
typedef custom_vector_like<element, MAX_ELEMENTS> data_array_type;
class ...
{
data_array_type m_data;
}
What would be considered a valid way to provide a safe "get data at idx" method for the component algo behavior.
Usually I would like to provide const and non-const ref to elements in the array, but how to deal with unexpected idx (out of bound)? Or how to deal with "find element that verifies X condition" if there is no find?
Would you use a reference parameter and return a bool for success (but then user code needs to define a element object to be filled in getter call...)?
Or just assert and let user (dev in debug at worse hopefully?) that he sent non-sense data as input?
Or maybe only provide access to the whole container (const or not) as ref and let user handle looping over / access members?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论