Overload operator

Discussion in 'C++' started by ek, May 16, 2007.

  1. ek

    ek Guest

    In the class below I overload the "()" operator. When reading an
    element in an int array "()" is therefore used. But why can't I still
    use "[]" when writing something like a[2] = 4;

    class MyArray {
    public:
    MyArray(int a) : data(new int[a]){}

    ~MyArray() {delete[] data;}

    int& operator()(int a){
    return data[a];
    }

    int operator()(int a) const{
    return data[a];
    }

    private:
    int* data;
    };

    int main() {

    MyArray Arr(4);
    Arr[2] = 5; // error: no match for 'operator[]' in 'Arr[2]'

    return 0;
    }

    When overloading "()" the normal "[]" operator no longer works. I can
    instead do the assignment with Arr(2) = 5; but I have not specified an
    overload of "()" that does assignment, why is that not needed?
    ek, May 16, 2007
    #1
    1. Advertising

  2. ek

    Zeppe Guest

    ek wrote:
    > In the class below I overload the "()" operator. When reading an
    > element in an int array "()" is therefore used. But why can't I still
    > use "[]" when writing something like a[2] = 4;


    you have to define the operator[] to use [].


    > When overloading "()" the normal "[]" operator no longer works.


    I doubt it would work even without operator() if you don't define the
    operator[]...

    Regards,

    Zeppe
    Zeppe, May 16, 2007
    #2
    1. Advertising

  3. On 16 Maj, 15:10, ek <> wrote:
    > In the class below I overload the "()" operator. When reading an
    > element in an int array "()" is therefore used. But why can't I still
    > use "[]" when writing something like a[2] = 4;
    >
    > class MyArray {
    > public:
    > MyArray(int a) : data(new int[a]){}
    >
    > ~MyArray() {delete[] data;}
    >
    > int& operator()(int a){
    > return data[a];
    > }
    >
    > int operator()(int a) const{
    > return data[a];
    > }
    >
    > private:
    > int* data;
    >
    > };


    > When overloading "()" the normal "[]" operator no longer works. I can
    > instead do the assignment with Arr(2) = 5; but I have not specified an
    > overload of "()" that does assignment, why is that not needed?


    This one takes care of the assignment:

    int& operator()(int a){
    return data[a];
    }

    It returns a reference to the element so when you assign to the
    reference you assign to the element.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, May 16, 2007
    #3
  4. ek

    James Kanze Guest

    On May 16, 3:10 pm, ek <> wrote:
    > In the class below I overload the "()" operator. When reading an
    > element in an int array "()" is therefore used. But why can't I still
    > use "[]" when writing something like a[2] = 4;


    You can. The two are completely orthogonal.

    > class MyArray {
    > public:
    > MyArray(int a) : data(new int[a]){}
    > ~MyArray() {delete[] data;}


    > int& operator()(int a){
    > return data[a];
    > }


    > int operator()(int a) const{
    > return data[a];
    > }


    > private:
    > int* data;
    > };


    > int main() {
    > MyArray Arr(4);
    > Arr[2] = 5; // error: no match for 'operator[]' in 'Arr[2]'


    That's because you didn't define one.

    > return 0;
    >
    > }


    > When overloading "()" the normal "[]" operator no longer works.


    What "normal []"? A class doesn't support "[]" unless you
    define it.

    > I can
    > instead do the assignment with Arr(2) = 5; but I have not specified an
    > overload of "()" that does assignment, why is that not needed?


    The non-const version of your operator() returns a reference.
    In C++ parlance, that means that the expression is an lvalue; in
    this case, an lvalue of type int. So you can assign to it.
    Since the reference actually refers to data[a], that's what gets
    assigned to.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 16, 2007
    #4
    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. Mr Smith
    Replies:
    7
    Views:
    455
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    464
    Jakob Bieling
    Aug 8, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    388
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    362
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

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

Share This Page