Link to home
Start Free TrialLog in
Avatar of doyston
doyston

asked on

Passing an object to a non-member function

Hi

I have written a fairly simply array library, using template-based objects which can be declared with

atype<int> myarray(20,30);

for an integer array object called "myarray" of 20 rows by 30 cols etc.  My query is, how do I pass this array object to a non-member function, in particular using a call-by-reference, so that the function can change the value of one of the array elements?  I'm not clear what to put in the function prototype, or the exact method of calling such a function.

Thanks in advance.
Doyston
Avatar of AlexFM
AlexFM

atype<int> myarray(20,30);

ChangeValue(myarray, 0, 0, 100);


void ChangeValue(atype& array, int n1, int n2, int nValue)
{
    array[n1][n2] = nValue;
}
Avatar of doyston

ASKER

Thanks AlexFM, but I'm still getting lots of compile errors (using g++).  If it's any help, here's the code snippet (and I've increased the points as I suspect this might be harder than it looks!).  Obviously, in reality the myfunc function is many times more complex, but this little program is just to test array object passing.

#include <cpparray.h>

void myfunc(int i, int j, atype& myarray);

int main(){
   int i, j, outval;

   // 20 rows x 30 cols, all values initialised to zero
   atype<int> myarray(20, 30);

   i=10;
   j=15;

   myfunc(i, j, myarray);

   outval = myarray(i,j); // Overloaded () etc

   cout << "Modified in function: " << outval << "\n";  

   return 0;
}

void myfunc(int i, int j, atype& myarray){
     
     cout << "row and col indices " << i << " " << j << "\n";
     myfunc(i,j) = 50;
}
Avatar of Mayank S
Since the function will be a non-member, so it must be declared as a friend in the class 'atype':

1. Keep a reference object as an argument in the function:

class atype
{
  ....
  ....
  friend <return-type> non_member ( atype &, .... <other arguments, if any> ) ;

} ; // class definition over


Notice that one of the arguments of this function will be a reference to an object of class 'atype'. That'll do it. To call the function, you just need to write:

non_member ( myarray, .... <other arguments, if any> ) ;

Individual elements of 'myarray' can be accessed in this function using myarray followed by the period (.) operator, followed by the name of the data member.


2. Or else, you can also keep a pointer to this object in the function as:


class atype
{
  ....
  ....
  friend <return-type> non_member ( atype *, .... <other arguments, if any> ) ;

} ; // class definition over

To call the function, you just need to write:

non_member ( &myarray, .... <other arguments, if any> ) ;

Individual elements of 'myarray' can be accessed in this function using myarray followed by the -> operator, followed by the name of the data member.

That'll do it!

Mayank.
Dear Doyston,

You are assigning:

myfunc ( i, j ) = 50 ;

myfunc () is the function, isn't it?

Shouldn't it be referring to: myarray ( i, j ) ?

What function are you using to store values into objects? Maybe something like:

void setvalue ( int i, int j, int val ) // inside the class
{
  // value at ( i, j ) set to val

} // end of setvalue

Then you can call it as:

myarray.setvalue ( i, j, 50 ) ;

in the function myfunc () where you've written myfunc ( i, j ) = 50 ;


Mayank.
Avatar of doyston

ASKER

Hi Mayank

Many apologies, yes in reality the key line in the myfunc function should be

myarray(i,j) = 50;

(In my enthusiam of editing down the large real function into something smaller to send to experts-exchange I'd made a typing error.)

In answer to your question about the function used to store values in the myarray object, I have overloaded the () symbols, so a separate function call isn't needed: as shown in the line above, the value 50 can be inserted directly into the correct position in the array object.

I'm VERY reluctant indeed to add friends into the atype class definition for extra functions, as this would force me to alter the cpparray.h library for every program I write.  All I want to do is pass my array object to another function, without having to have that non-member function coded into the class definition.

Thanks in advance for your advice.
Can you inherit the class 'atype' and create a new derived class, say, 'myarrayclass' which will declare this function myfunc () as a friend? And instead of declaring the object myarray of type 'atype', you can declare it to be an object of 'myarrayclass' instead of 'atype'. Include member functions in that class to set/ get values of data-members, which should call the functions in the 'atype' base class. That should solve the problem.

Mayank.
Can you inherit the class 'atype' and create a new derived class, say, 'myarrayclass' which will declare this function myfunc () as a friend? And instead of declaring the object myarray of type 'atype', you can declare it to be an object of 'myarrayclass' instead of 'atype'. Include member functions in that class to set/ get values of data-members, which should call the functions in the 'atype' base class. That should solve the problem.

Mayank.
ASKER CERTIFIED SOLUTION
Avatar of jadams117
jadams117

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doyston

ASKER

Brilliant!! - I've used the format

template <class T>
void myfunc(int i, int j, atype<T>& myarray){
   
   cout << "row and col indices " << i << " " << j << "\n";
   myarray(i,j) = 50;
}

as you suggest and everything works perfectly!  Thanks again for your help.