对象作为数组索引

3

我有以下的类:

class MyInteger
{
private:
    __int64 numero;

    static __int64 int64Pow(__int64, __int64);
public:
    // It doesn't matter how these methods are implemented
    friend class MyInteger;
    MyInteger(void);
    MyInteger(const MyInteger&);
    MyInteger(const __int64&);
    ~MyInteger(void);

    static MyInteger const minValue;
    static MyInteger const maxValue;

    MyInteger& operator = (const MyInteger&);
    MyInteger operator + (const MyInteger&) const;
    MyInteger operator - (const MyInteger&) const;
    MyInteger operator * (const MyInteger&) const;
    MyInteger operator / (const MyInteger&) const;
    MyInteger& operator += (const MyInteger&);
    MyInteger& operator -= (const MyInteger&);
    MyInteger& operator *= (const MyInteger&);
    MyInteger& operator /= (const MyInteger&);
    MyInteger operator % (const MyInteger&) const;
    MyInteger& operator %= (const MyInteger&);
    MyInteger& operator ++ ();
    MyInteger operator ++ (int);
    MyInteger& operator -- ();
    MyInteger operator -- (int);
    bool operator == (const MyInteger&) const;
    bool operator != (const MyInteger&) const;
    bool operator > (const MyInteger&) const;
    bool operator < (const MyInteger&) const;
    bool operator >= (const MyInteger&) const;
    bool operator <= (const MyInteger&) const;

    int toStdInt() const
    {
        return (int)numero;
    }
    float toStdFloat() const;
    double toStdDouble() const;
    char toStdChar() const;
    short toStdShortInt() const;
    long toStdLong() const;
    long long toStdLongLong() const;
    unsigned int toStdUInt() const;
    __int64 toStdInt64() const;
    unsigned __int64 toStdUInt64() const;
    unsigned long long int toStdULongLong() const;
    long double toStdULongDouble() const;

    template<class Type>
    Type& operator[](Type* sz)
    {
        return sz[toStdULongLong()];
    }
};

template<class Type>
Type* operator+(const Type* o1, const MyInteger& o2)
{
    return ((o1) + (o2.toStdInt()));
}

我想使用这个类来像这样访问数组元素:
MyInteger myInt(1);
int* intPtr = (int*)malloc(sizeof(int) * N);
intPtr[myInt] = 1;

我认为这个函数
template<class Type>
Type* operator+(const Type* o1, const MyInteger& o2)
{
    return ((o1) + (o2.toStdInt()));
}

我希望你能帮我翻译一下与 IT 技术有关的内容。以下是需要翻译的内容:

我想解决我的问题,因为正如这篇文章所报道的那样(C++ 中数组索引的类型),“表达式 E1[E2] 等同于 *((E1)+(E2))”,但是我遇到了 C2677 错误('[' 运算符:没有接受 'MyInteger' 类型的全局运算符(或者没有可接受的转换))。

请问有人能够为我澄清这种情况吗?谢谢。


std::unordered_map? - Some programmer dude
@JoachimPileborg 感谢您的回答,但这不是我正在寻找的。 - Matteo Umili
1个回答

3
您可以通过类似以下方式覆盖您的MyInteger类的int转换来实现这一点:
class MyInteger {
   ...

   operator int() const
   {
       return toStdInt(); /** Your class as an array index (int) */
   }

   ...
}

我已经尝试过了,但是当我做类似以下的操作时: `MyInteger i(0); i + 1;`我会得到错误 C2666: 'MyInteger::operator +': 2 overloads have similar conversions。 - Matteo Umili
其实我认为你不必覆盖所有的数学运算符,你只需要覆盖整数转换和二进制运算符就可以自动工作了。不过我可能是错的。 - Lake
1
这不是一个强制类型转换。它是一个转换运算符。强制类型转换是你在源代码中编写的内容,用于告诉编译器进行转换。 - Pete Becker
1
i + 1 可以被看作是 i + MyInterger(1) 或者 static_cast<int>(i) + 0。你可以通过在 "int 构造函数" 中添加 explicit 来禁止隐式的 i + MyInterger(1),或者重载所有二元运算符来解决这个问题... - Jarod42
1
@Lake 看起来它能够工作,谢谢。有人能解释一下为什么我尝试的方法没有起作用吗? - Matteo Umili
@codroipo 你尝试的方法之所以不起作用很简单:正如你所说,在C语言中,a[i]在编译后映射为*(a+i)。问题在于,在代码转换为其二进制表示之前,编译器会使用数组查找“原样”进行类型检查,并且有一个特定的规则,即用作查找索引的每个表达式都必须推断出整数类型。我猜如果你明确地使用*(myInt + intPtr)而不是intPtr[myInt],它应该可以工作。 - Lake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接