当实现operator []时,我应该如何包括边界检查? [英] When implementing operator[] how should I include bounds checking?

查看:288
本文介绍了当实现operator []时,我应该如何包括边界检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,对于这样一个简单的问题的长期领导,我道歉。

First of all I apologize for the long lead up to such a simplistic question.

我实现一个类作为一个非常长的1维索引空间填充曲线或表示该索引对应的笛卡尔坐标的n元组。

I am implementing a class which serves as a very long 1 dimensional index on a space filling curve or the n-tuple representing the Cartesian coordinate that index corresponds to.

class curvePoint
{
public:
    friend class curveCalculate;

    //Construction and Destruction
    curvePoint(): point(NULL), dimensions(0) {}
    virtual ~curvePoint(){if(point!=NULL) delete[] point;}

    //Mutators
    void convertToIndex(){ if(isTuple()) calc(this); }
    void convertToTuple(){ if(isIndex()) calc(this); }
    void setTuple(quint16 *tuple, int size);
    void setIndex(quint16 *index, int size);
    void setAlgorithm(curveType alg){algorithm = alg;}

    //Inspectors
    bool isIndex(){return current==Index;}
    bool isTuple(){return current==Tuple;}
    size_t size(){return dimensions;}
    quint16 operator[](size_t index);

    enum curveType{HilbertCurve, ZCurve, GrayCodeCurve};
    enum status{Index, Tuple};

private:
    curveCalculate calc;
    curveType algorithm;
    quint16 *point;
    size_t dimensions;
    status current;
};

指向的数组的长度为尺寸

(The length of the array pointed to by point is dimensions)

无论如何,在实现operator []时,我想知道什么是最好的方法来实现边界检查。我想避免抛出异常,如果可能的话,全范围的值可用于数组中的每个数字,所以如果超出边界错误,返回一个特殊的值是不可能的;

Anyways in the implementation of operator[] I was wondering what the best method to achieve bounds checking is. I want to avoid throwing exceptions if at all possible, and the full range of values is usable for each number in the array so a special value to return in case of an out of bounds error is not possible either;

我在想类似这样的东西,但实现在类定义:

I was thinking of something like this though implemented in the class definition:

quint16 curvePoint::operator[](size_t index)
{
    return point[ index % dimensions ];
}

这使得我们永远不会离开数组的边界记录我认为会很好;

This makes it so that we never leave the bounds of the array and if well documented I think it would be fine; nevertheless, I am leary of this particular implementation.

这看起来可以接受别人吗?
在仍然满足我的约束的情况下,有没有其他方法进行边界检查?

Does this look acceptable to others? Is there any other way of doing bounds checking while still satisfying my constraints?

编辑:
计算Hilbert曲线等东西是非常混乱,乱码,我不想要的stl库的额外的接口的方式。

Calculation of things like Hilbert curves etc are highly messy, messy enough that I do not not want the additional interface for the stl libraries in the way.

此外,因为我将不得不每次转换成千上万的这些多维数据库查询我不想在混合中的stl函数调用的额外成本,如果可能的话。

Additionally because I will have to convert many thousands of these every time the multidimensional database is queried I do not want the additional cost of the stl function calls in the mix, if at all possible.

我更喜欢assert的想法;但是,如果我记得正确,发布版本中的中断不会吗?

I rather like the idea of the assert; but, if I remember correctly that breaks in release builds does it not?

我想我可以使用异常,这似乎是每个人都在生根,但我使用Qt库和那些避免异常的性能和可移植性,我希望做同样的。

I suppose I can use exceptions, that seems to be what everyone is rooting for, but I am using the Qt libraries and those avoid exceptions for both performance and portability and I was hoping to do the same.

推荐答案

是做C ++本身。这限制了您的用户会遇到的惊喜。

The easiest solution is to do as C++ itself does. This limits the amount of surprises that your users will experience.

C ++本身是相当一致的。指针上的内置 [] std :: vector :: operator [] 有未定义的行为,如果你使用一个超出范围的数组索引。如果你想要边界检查,是显式的,并使用 std :: vector :: at

C++ itself is fairly consistent. Both the built-in [] on pointers and std::vector::operator[] have undefined behavior if you use an out-of-bound array index. If you want bounds checking, be explicit and use std::vector::at

对于你的类,同样的,你可以将超越行为记录为标准。

Hence, if you do the same for your class, you can document the out-of-bound behavior as "standard".

这篇关于当实现operator []时,我应该如何包括边界检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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