3

I have the following class:

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()));
}

I'd like to use this class to access array elements like this:

MyInteger myInt(1);
int* intPtr = (int*)malloc(sizeof(int) * N);
intPtr[myInt] = 1;

I thought that the function

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

could solve my problem, because as this post reports (Type of array index in C++) "The expression E1[E2] is identical (by definition) to *((E1)+(E2))", but I get the C2677 error ('[' operator: no global operator found which takes type 'MyInteger' (or there is no acceptable conversion))

Can someone clarify me this situation? Thanks

2
  • std::unordered_map? Commented Sep 12, 2013 at 12:52
  • @JoachimPileborg Thanks for the answer, but it isn't what I'm looking for Commented Sep 12, 2013 at 13:34

1 Answer 1

3

You may be able to do that by overriding the cast to int of your MyInteger class in a way similar to:

class MyInteger {
   ...

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

   ...
}
Sign up to request clarification or add additional context in comments.

6 Comments

I already tried, but when I do something like MyInteger i(0); i + 1; I get error C2666: 'MyInteger::operator +': 2 overloads have similar conversions
Actually I don't think you have to override all the mathematical operators, you could just override the cast to integer and binary operators should work automatically.. I might be wrong though.
This is not a cast. It's a conversion operator. A cast is something you write in your source code to tell the compiler to do a conversion.
i + 1 may be seen as i + MyInterger(1) or static_cast<int>(i) + 0. You may solve it by Adding explicit to your "int constructor" to forbid the implicit i + MyInterger(1) or overload all binary operator...
@Lake It seems to work, thank you. Can someone explain me also why the method I tried didn't work?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.