Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it possible to use object functions as arguments to theinner_product algorithm?

Reply
Thread Tools

Is it possible to use object functions as arguments to theinner_product algorithm?

 
 
Craig Sanders
Guest
Posts: n/a
 
      03-22-2010
Hello all.

I'm hoping that someone might please be able to help me out.

Does anyone know if it is possible to pass object member functions (as
opposed to class member functions) as the 4th and 5th arguments to the
inner_product function?

class
TestClass
{
// Various function declarations omitted for brevity.

int sumFunction(int a, int b)
{
return(a + b);
}

int productFunction(int a, int b)
{
return(a * b);
}
};

int main(void)
{
// Assume vectorA and vectorB are created and populated with
values.

TestClass testClass;


cout << "Result = " <<
inner_product
(
vectorA.begin(),
vectorA.end(),
vectorB.begin(),
testClass.sumFunction,
testClass.productFunction
) <<
endl;

return(0);
}

I can't seem to get main to compile for the case shown above. However,
if sumFunction and productFunction are turned into class functions,
i.e. by prepending their definition with the keyword static, then I
can get main to compile and work. Is anyone able to tell me why this
is the case and how I can get inner_product to work with object
functions?

Thanks in advance.

- Craig
 
Reply With Quote
 
 
 
 
Michael Doubez
Guest
Posts: n/a
 
      03-22-2010
On 22 mar, 11:05, Craig Sanders <(E-Mail Removed)> wrote:
> Hello all.
>
> I'm hoping that someone might please be able to help me out.
>
> Does anyone know if it is possible to pass object member functions (as
> opposed to class member functions) as the 4th and 5th arguments to the
> inner_product function?
>
> class
> TestClass
> {
> * * // Various function declarations omitted for brevity.
>
> * * int sumFunction(int a, int b)
> * * {
> * * * * return(a + b);
> * * }
>
> * * int productFunction(int a, int b)
> * * {
> * * * * return(a * b);
> * * }
>
> };
>
> int main(void)
> {
> * * // Assume vectorA and vectorB are created and populated with
> values.
>
> * * TestClass * testClass;
>
> * * cout << "Result = " <<
> * * inner_product
> * * (
> * * * * vectorA.begin(),
> * * * * vectorA.end(),
> * * * * vectorB.begin(),
> * * * * testClass.sumFunction,
> * * * * testClass.productFunction
> * * ) <<
> * * endl;
>
> * * return(0);
>
> }
>
> I can't seem to get main to compile for the case shown above. However,
> if sumFunction and productFunction are turned into class functions,
> i.e. by prepending their definition with the keyword static, then I
> can get main to compile and work. Is anyone able to tell me why this
> is the case


&TestClass::sumFunction and &TestClass:roductFunction are member
function pointer an unusable without a TestClass.

Looking at your functions, they should even be free functions.

> and how I can get inner_product to work with object
> functions?


In the current standard there is no generic bind function to construct
a binary function object (like bind1st for unary).

You can either make your own or use an existing binding library (such
as Boost.Bind).

--
Michael
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-22-2010
On 22 Mar, 10:05, Craig Sanders <(E-Mail Removed)> wrote:

> I'm hoping that someone might please be able to help me out.


> Does anyone know if it is possible to pass object member
> functions (as opposed to class member functions) as the 4th
> and 5th arguments to the inner_product function?


> class
> TestClass
> {
> // Various function declarations omitted for brevity.


> int sumFunction(int a, int b)
> {
> return(a + b);
> }


> int productFunction(int a, int b)
> {
> return(a * b);
> }
> };


> int main(void)
> {
> // Assume vectorA and vectorB are created and populated with values.


> TestClass testClass;


> cout << "Result = " <<
> inner_product
> (
> vectorA.begin(),
> vectorA.end(),
> vectorB.begin(),
> testClass.sumFunction,
> testClass.productFunction
> ) <<
> endl;
>
> return(0);
> }


> I can't seem to get main to compile for the case shown above.
> However, if sumFunction and productFunction are turned into
> class functions, i.e. by prepending their definition with the
> keyword static, then I can get main to compile and work. Is
> anyone able to tell me why this is the case and how I can get
> inner_product to work with object functions?


The definition of inner_product is that it does:
acc = binary_op1(acc, binary_op2(*i1, *i2));
where acc is the current accumulator value, i1 and i2 are the
current iterators, and binary_op1 and binary_op2 are the
functions you passed. That expression obviously isn't legal for
non-static member functions.

It's not too clear what you actually want. Given the function
definitions you present, the functions shouldn't even be members
(static or otherwise); they have nothing to do with the class.
And as members, the obviously cannot be used in the above, in
any way, because they require three arguments: the object
they're called on and the two int's. If you actually have a
member function with a single argument (in addition to the
implicit this), and you want to call it on one of the iterators,
something like:
acc = binary_op1(acc, (*i1).binary_op2(*i2));
, then some of the adaptors (mem_fun, etc.) might be able to
help. If, on the other hand, your function actually takes two
arguments (in addition to the implicit this pointer), and uses
members of the class in addition to the two parameters, you'll
have to define a separate helper class, along the lines of:

class Product
{
TestClass* my_owner;
public:
explicit Product(TestClass& owner)
: my_owner(&owner) {}
int operator()(int a, int b) const
{
return my_owner->sumFunction(a, b);
}
};

, and passing an instance of it:
int result
= inner_product(a.begin(), a.end(), b.begin(), 0,
Sum(testClass), Product(testClass));

(Think too about making the functions and the pointer in the
above const.)

--
James Kanze
 
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
Is it possible to use keyword arguments from C? Darren Dale Python 1 07-13-2009 01:47 PM
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
Keyword arguments in a block, possible with zero arguments? Peter Motzfeldt Ruby 1 03-13-2007 01:33 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM



Advertisments