?
Solved

Passing an object to a non-member function

Posted on 2003-03-13
9
Medium Priority
?
232 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:doyston
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8126111
atype<int> myarray(20,30);

ChangeValue(myarray, 0, 0, 100);


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

Author Comment

by:doyston
ID: 8126249
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;
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8126348
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8126373
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.
0
 

Author Comment

by:doyston
ID: 8126659
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8126690
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8126739
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.
0
 
LVL 1

Accepted Solution

by:
jadams117 earned 1400 total points
ID: 8126923
The function declaration you use needs to specify the template argument as this is part of the class-name. You can do this for a specific template class or declare another template argument for the function. E.g.

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

or..

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

Author Comment

by:doyston
ID: 8126982
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question