Overload Operators for referenced objects?

Discussion in 'C++' started by Jonas Huckestein, Feb 11, 2007.

  1. hello,

    somehow i can't figure out, how to overload the [] operator for a referenced
    object. if i have

    class MyClass {

    int operator[](int i) { return 1; };

    };

    ....

    MyClass* oskar = new MyClass();
    cout << oskar[3];
    delete oskar;

    the compiler says "cannot convert ‘OperatorSequence’ to ‘int’ in
    initialization". with

    MyClass oskar;

    it works, though. how can i use the overloaded [] for pointers? i also need
    to overload arithmetic operations in my class, is there a similar problem
    when dealing with referenced objects?

    thanks in advance and greetings, jonas
     
    Jonas Huckestein, Feb 11, 2007
    #1
    1. Advertising

  2. Jonas Huckestein

    Rolf Magnus Guest

    Jonas Huckestein wrote:

    > hello,
    >
    > somehow i can't figure out, how to overload the [] operator for a
    > referenced object. if i have
    >
    > class MyClass {
    >
    > int operator[](int i) { return 1; };
    >
    > };
    >
    > ...
    >
    > MyClass* oskar = new MyClass();
    > cout << oskar[3];
    > delete oskar;
    >
    > the compiler says "cannot convert ‘OperatorSequence’ to ‘int’ in
    > initialization".


    The compiler probably wants to say that it can't find an operator << that
    takes a MyClass object. oskar[3] ist the same as *(oskar+3), expecting that
    oskar is the pointer to the start of an array of MyClass, and you wan the
    3rd element of that array.

    > with
    >
    > MyClass oskar;
    >
    > it works, though. how can i use the overloaded [] for pointers?


    You are using it, but I guess you actually don't want to. If you want to use
    the [] operator of your class instead of the one for pointers, you have to
    dereference first:

    cout << (*oskar)[3];

    > i also need to overload arithmetic operations in my class, is there a
    > similar problem when dealing with referenced objects?


    Well, if you use a pointer, the operators for pointers will be used.
     
    Rolf Magnus, Feb 11, 2007
    #2
    1. Advertising

  3. Jonas Huckestein

    Kai-Uwe Bux Guest

    Jonas Huckestein wrote:

    > hello,
    >
    > somehow i can't figure out, how to overload the [] operator for a
    > referenced object. if i have
    >
    > class MyClass {
    >
    > int operator[](int i) { return 1; };


    You might want to consider using std::size_t instead of int as the argument
    type.

    >
    > };
    >
    > ...
    >
    > MyClass* oskar = new MyClass();
    > cout << oskar[3];


    Two options:

    oskar->operator[](3)

    or (more readable):

    (*oskar)[3]


    > delete oskar;
    >
    > the compiler says "cannot convert ?OperatorSequence? to ?int? in
    > initialization". with
    >
    > MyClass oskar;
    >
    > it works, though. how can i use the overloaded [] for pointers? i also
    > need to overload arithmetic operations in my class, is there a similar
    > problem when dealing with referenced objects?


    The problem is that you don't distinguish between the pointer and the
    pointee. If you want to add the pointees, you have to get them first by
    dereferencing the pointer. The problem is totally unrelated to your class,
    you would face it with pointers of type int* just as well.


    int* ap = new int ( 5 );
    int* bp = new int ( 6 ); // warning: already possibly leaking ap.
    ap + bp; // bogus: trying to add two pointers
    *ap + *bp; // ok: adding pointees.
    delete ap;
    delete bp;


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 11, 2007
    #3
  4. Jonas Huckestein

    red floyd Guest

    Jonas Huckestein wrote:
    > hello,
    >
    > somehow i can't figure out, how to overload the [] operator for a referenced
    > object. if i have
    >
    > class MyClass {
    >
    > int operator[](int i) { return 1; };
    >
    > };
    >
    > ...
    >
    > MyClass* oskar = new MyClass();
    > cout << oskar[3];
    > delete oskar;
    >
    > the compiler says "cannot convert ‘OperatorSequence’ to ‘int’ in
    > initialization". with
    >
    > MyClass oskar;
    >
    > it works, though. how can i use the overloaded [] for pointers? i also need
    > to overload arithmetic operations in my class, is there a similar problem
    > when dealing with referenced objects?
    >
    > thanks in advance and greetings, jonas
    >
    >

    Because you're applying it to a pointer, not a reference or an object.

    MyClass oskar;
    cout << oskar[3];
     
    red floyd, Feb 11, 2007
    #4
  5. Jonas Huckestein wrote:
    > hello,
    >
    > somehow i can't figure out, how to overload the [] operator for a referenced
    > object. if i have
    >
    > class MyClass {
    >
    > int operator[](int i) { return 1; };
    >
    > };
    >
    > ...
    >
    > MyClass* oskar = new MyClass();
    > cout << oskar[3];
    > delete oskar;
    >
    > the compiler says "cannot convert ‘OperatorSequence’ to ‘int’ in
    > initialization". with
    >
    > MyClass oskar;
    >
    > it works, though. how can i use the overloaded [] for pointers? i also need
    > to overload arithmetic operations in my class, is there a similar problem
    > when dealing with referenced objects?
    >
    > thanks in advance and greetings, jonas
    >
    >


    You cannot overload operator[] for pointers, you cannot overload any
    pointer operator.

    It sounds like you want a class that overload several operators but
    behaves like a pointer otherwise. Well, that is what you must write

    class MyPtrClass
    {
    public:
    int operator[](int i);
    // etc...
    private:
    MyClass* ptr;
    };

    In other words write a class that contains a single pointer to another
    class, and put the overloaded operators there.

    john
     
    John Harrison, Feb 11, 2007
    #5
  6. "Kai-Uwe Bux" <> wrote in message
    news:eqnnnc$gi7$...
    > Jonas Huckestein wrote:
    >
    >> hello,
    >>
    >> somehow i can't figure out, how to overload the [] operator for a
    >> referenced object. if i have
    >>
    >> class MyClass {
    >>
    >> int operator[](int i) { return 1; };

    >
    > You might want to consider using std::size_t instead of int as the
    > argument
    > type.


    Yes you would think that, although in some cases this can lead to problems,
    particularly when a conversion operator to another type that supports
    operator[](int) exists (such as pointers). Say, for example, you have a
    string class with a conversion operator to const char *

    class String
    {
    public:
    // yadda yadda yadda
    char & operator[](size_t);
    char operator[](size_t) const;

    operator const char *() const;
    };

    void foo()
    {
    String s;
    s[4]; // ambiguous call
    }

    s[4] wouldn't be ambiguous if String::eek:perator[] accepted int instead of
    size_t, because the literal 4 is int, which either has to be promoted to
    size_t for String::eek:perator[], or s has to be converted to const char* for
    operator[](const char*, int). Now, if String::eek:perator[] accepted int,
    you'll never have this problem (not even if you pass it size_t).

    I think it's best to leave the argument to operator[]s for array-like
    indexing as ints (or perhaps even better: ptrdiff_t) rather than as other
    integral types, as that's how the standard defines it's built-in types.

    - Sylvester
     
    Sylvester Hesp, Feb 12, 2007
    #6
  7. Jonas Huckestein

    Kai-Uwe Bux Guest

    Sylvester Hesp wrote:

    >
    > "Kai-Uwe Bux" <> wrote in message
    > news:eqnnnc$gi7$...
    >> Jonas Huckestein wrote:
    >>
    >>> hello,
    >>>
    >>> somehow i can't figure out, how to overload the [] operator for a
    >>> referenced object. if i have
    >>>
    >>> class MyClass {
    >>>
    >>> int operator[](int i) { return 1; };

    >>
    >> You might want to consider using std::size_t instead of int as the
    >> argument
    >> type.

    >
    > Yes you would think that, although in some cases this can lead to
    > problems, particularly when a conversion operator to another type that
    > supports operator[](int) exists (such as pointers). Say, for example, you
    > have a string class with a conversion operator to const char *
    >
    > class String
    > {
    > public:
    > // yadda yadda yadda
    > char & operator[](size_t);
    > char operator[](size_t) const;
    >
    > operator const char *() const;
    > };
    >
    > void foo()
    > {
    > String s;
    > s[4]; // ambiguous call
    > }
    >
    > s[4] wouldn't be ambiguous if String::eek:perator[] accepted int instead of
    > size_t, because the literal 4 is int, which either has to be promoted to
    > size_t for String::eek:perator[], or s has to be converted to const char* for
    > operator[](const char*, int). Now, if String::eek:perator[] accepted int,
    > you'll never have this problem (not even if you pass it size_t).
    >
    > I think it's best to leave the argument to operator[]s for array-like
    > indexing as ints (or perhaps even better: ptrdiff_t) rather than as other
    > integral types, as that's how the standard defines it's built-in types.


    Well, I beg to differ. I agree that the example demonstrates a problem.
    However, I would locate the problem in the example at a different position:
    the conversion operator to char* should be made explicit, i.e., it should
    be a member function like data() or c_str(). This is the precedent set by
    the standard library. Generally, implicit conversions can lead to
    surprises, and this is what your example demonstrates.

    The standard library makes operator[] take size_type for container types and
    difference_type for iterator types, which makes perfect sense. Note that
    raw-pointers are more like iterators than containers. The class in you
    example may suffer from not knowing what it wants to be. By and large, I
    tend to follow the standard library in those questions. It makes my code
    base more coherent.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 12, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    383
    Nick Hounsome
    Jan 25, 2004
  2. Suman
    Replies:
    5
    Views:
    2,795
    Chris Theis
    Jun 14, 2006
  3. Replies:
    2
    Views:
    528
    dasjotre
    Feb 22, 2007
  4. ThomasW
    Replies:
    11
    Views:
    298
    ThomasW
    Sep 28, 2009
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    273
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page