overload function call operator

Discussion in 'C++' started by jr.freester@gmail.com, Nov 3, 2008.

  1. Guest

    I've written a Matrix container class and overloaded the function call
    operator to return values at a specified index.
    Below is the member function

    double operator()(int a , int b)
    {
    if((a < 1) || (b < 1) || ((a + 1) > this->row) || ((b + 1) > this-
    >col))

    {
    cerr << "Invalid index for Matrix" <<endl;
    }

    return this->data[(a-1)*this->col + (b-1)];
    }

    It is invoked
    int row, col;
    double d;
    Matrix M;
    d = M(row,col);

    My question is thus, can I accomplish the reverse task of assigning a
    double d to a Matrix M at power row,col ie.
    M(row,col) = d;

    Justin
     
    , Nov 3, 2008
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    wrote:

    > I've written a Matrix container class and overloaded the function call
    > operator to return values at a specified index.
    > Below is the member function
    >
    > double operator()(int a , int b)


    Probably, it is not the best idea to allow signed arguments.

    > {
    > if((a < 1) || (b < 1) || ((a + 1) > this->row) || ((b + 1) > this-
    >>col))


    Probably, it would be better to go with the C convention to start indexing
    at 0. This will get rid of many "-1" in the code, which are prone to error.

    > {
    > cerr << "Invalid index for Matrix" <<endl;


    This should be an assert().

    In an at()-method, it you would throw something.

    > }
    >
    > return this->data[(a-1)*this->col + (b-1)];
    > }
    >
    > It is invoked
    > int row, col;
    > double d;
    > Matrix M;
    > d = M(row,col);
    >
    > My question is thus, can I accomplish the reverse task of assigning a
    > double d to a Matrix M at power row,col ie.
    > M(row,col) = d;


    Yes, return a reference

    double & operator()( size_type a, size_type b );


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 3, 2008
    #2
    1. Advertising

  3. Guest

    On Nov 3, 6:58 pm, Kai-Uwe Bux <> wrote:
    > wrote:
    > > I've written a Matrix container class and overloaded the function call
    > > operator to return values at a specified index.
    > > Below is the member function

    >
    > > double operator()(int a , int b)

    >
    > Probably, it is not the best idea to allow signed arguments.
    >
    > > {
    > >     if((a < 1) || (b < 1) || ((a + 1) > this->row) || ((b + 1) > this-
    > >>col))

    >
    > Probably, it would be better to go with the C convention to start indexing
    > at 0. This will get rid of many "-1" in the code, which are prone to error.
    >
    > >     {
    > >         cerr << "Invalid index for Matrix" <<endl;

    >
    > This should be an assert().
    >
    > In an at()-method, it you would throw something.
    >
    > >     }

    >
    > >     return this->data[(a-1)*this->col + (b-1)];
    > > }

    >
    > > It is invoked
    > > int row, col;
    > > double d;
    > > Matrix M;
    > > d = M(row,col);

    >
    > > My question is thus,  can I accomplish the reverse task of assigning a
    > > double d to a Matrix M at power row,col ie.
    > > M(row,col) = d;

    >
    > Yes, return a reference
    >
    >   double & operator()( size_type a, size_type b );
    >
    > Best
    >
    > Kai-Uwe Bux


    Kai-Uwe, thank you for your quick response, but I am having trouble
    understanding the last statement.
    > Yes, return a reference
    >
    > double & operator()( size_type a, size_type b );


    what would the body of this function look like? I don't understand
    how adding the reference operator changes the order of assignment of
    M(a,b) -> d TO d -> M(a,b). Any additional help would be
    appreciated.

    Justin
     
    , Nov 4, 2008
    #3
  4. Kai-Uwe Bux Guest

    wrote:

    > On Nov 3, 6:58 pm, Kai-Uwe Bux <> wrote:
    >> wrote:
    >> > I've written a Matrix container class and overloaded the function call
    >> > operator to return values at a specified index.
    >> > Below is the member function

    >>
    >> > double operator()(int a , int b)

    >>
    >> Probably, it is not the best idea to allow signed arguments.
    >>
    >> > {
    >> > if((a < 1) || (b < 1) || ((a + 1) > this->row) || ((b + 1) > this-
    >> >>col))

    >>
    >> Probably, it would be better to go with the C convention to start
    >> indexing at 0. This will get rid of many "-1" in the code, which are
    >> prone to error.
    >>
    >> > {
    >> > cerr << "Invalid index for Matrix" <<endl;

    >>
    >> This should be an assert().
    >>
    >> In an at()-method, it you would throw something.
    >>
    >> > }

    >>
    >> > return this->data[(a-1)*this->col + (b-1)];
    >> > }

    >>
    >> > It is invoked
    >> > int row, col;
    >> > double d;
    >> > Matrix M;
    >> > d = M(row,col);

    >>
    >> > My question is thus,  can I accomplish the reverse task of assigning a
    >> > double d to a Matrix M at power row,col ie.
    >> > M(row,col) = d;

    >>
    >> Yes, return a reference
    >>
    >> double & operator()( size_type a, size_type b );
    >>
    >> Best
    >>
    >> Kai-Uwe Bux

    >
    > Kai-Uwe, thank you for your quick response, but I am having trouble
    > understanding the last statement.
    >> Yes, return a reference
    >>
    >> double & operator()( size_type a, size_type b );

    >
    > what would the body of this function look like?


    e.g.:

    double & operator()( size_type a, size_type b ) {
    assert( a < this->row );
    assert( b < this->col );
    return( this->data[ a * this->col + b ] );
    }

    > I don't understand
    > how adding the reference operator changes the order of assignment of
    > M(a,b) -> d TO d -> M(a,b).


    It doesn't. It affects how expressions in the return statement are
    interpreted. In this case,

    this->data[ a * this->col + b ]

    is interpreted as a reference. Note that by and in itself,

    this->data[ a * this->col + b ]

    is an lvalue. In particular,

    this->data[ a * this->col + b ] = some_thing;

    would be well-formed. That magic can be wrapped up in a reference and be
    returned.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Nov 4, 2008
    #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. Piotre Ugrumov
    Replies:
    3
    Views:
    374
    Nick Hounsome
    Jan 25, 2004
  2. Fei Liu
    Replies:
    5
    Views:
    348
    Maxim Yegorushkin
    Feb 25, 2006
  3. Layton
    Replies:
    2
    Views:
    458
    Layton
    Sep 27, 2006
  4. Mahain
    Replies:
    3
    Views:
    1,019
    James Kanze
    Dec 20, 2007
  5. Ying-Chieh Liao

    function overload (not operator overload)

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

Share This Page