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

Discussion in 'C++' started by Craig Sanders, Mar 22, 2010.

  1. 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
    Craig Sanders, Mar 22, 2010
    #1
    1. Advertising

  2. On 22 mar, 11:05, Craig Sanders <> 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::productFunction 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
    Michael Doubez, Mar 22, 2010
    #2
    1. Advertising

  3. Craig Sanders

    James Kanze Guest

    On 22 Mar, 10:05, Craig Sanders <> 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
    James Kanze, Mar 22, 2010
    #3
    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. Ahmed Moustafa
    Replies:
    0
    Views:
    776
    Ahmed Moustafa
    Nov 15, 2003
  2. Bapaiah Katepalli
    Replies:
    1
    Views:
    1,495
    Mike Treseler
    Jun 23, 2006
  3. Darren Dale
    Replies:
    1
    Views:
    542
    Darren Dale
    Jul 13, 2009
  4. Peter Motzfeldt
    Replies:
    1
    Views:
    147
  5. oldyork90
    Replies:
    10
    Views:
    346
    Jorge
    Sep 27, 2008
Loading...

Share This Page