Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > overload function call operator

Reply
Thread Tools

overload function call operator

 
 
jr.freester@gmail.com
Guest
Posts: n/a
 
      11-03-2008
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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-03-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
jr.freester@gmail.com
Guest
Posts: n/a
 
      11-04-2008
On Nov 3, 6:58*pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-04-2008
(E-Mail Removed) wrote:

> On Nov 3, 6:58*pm, Kai-Uwe Bux <(E-Mail Removed)> wrote:
>> (E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can we overload '=' operator using friend function c++ Mahain C++ 3 12-20-2007 06:29 PM
friend declaration or operator overload function cross namespace. Layton C++ 2 09-27-2006 10:38 PM
function template and operator () overload Fei Liu C++ 5 02-25-2006 12:28 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments