6

What I want to do is:

class A
{
    public:
    double sum(double a, double b);
    double max(double a, double b);
}

template <typename T>
class B
{
    std::vector<T> data;

    public:

    double sum (double a, double b);
    double max (double a, double b);
    double average( MyFunction, double a, dobule b)
    {
        double sum = 0;
        int n = data.size();

        for ( int i = 0; i < n; i++)
            sum = sum + data[i].MyFunction(double a, double b)

            return sum / n;
    }

}

example:

double average( max, double a, double b)
{
    double sum = 0;
    int n = data.size();

    for ( int i = 0; i < n; i++)
        sum = sum + data[i].max(double a, double b)

        return sum / n;
}

Why?

  1. it would save me to write function like: average of sum. average of max. average of min which are all pretty similar functions.
  2. the way it is coded B< B< A> > works

What have I tried?

  • function pointer
  • S1:

      typedef double (A::*MyFunctionf)(double, double);
      typedef double (B<A>::*MyFunctionff)(double, double);
      typedef double (B<B<A> >::*MyFunctionfff)(double, double);
    
    • it Works. Problems:
      • it is not beautiful to declare 3-4 typedef of function pointer
      • if I want to write the function inside B that sent a function pointer it will be hard coded and only 1 of the 3 typedef can be hard coded. Meaning: it is not working for every cases
  • S2 (based on Template typedefs - What's your work around?):

      template <typename rtype, typename t>
      struct CallTemplate
      {
        typedef rtype (t::*ptr)(double, double);
      };
    
     // the function becomes :
     template <typename T>
     template<typename rtype>
     double B<T>::average(CallTemplate<double, T> MyFunction, double a, double b)
     {
        double sum = 0;
       int n = data.size();
    
          for ( int i = 0; i < n; i++)
            sum = sum + (data[i].*MyFunction)( a, b)
    
        return sum / n;
      }
    

    example:

     // makes an error "Myfunction was not declared" + " 
     // dependent-name'{anonymous}::CallTemplate<double, A>::ptr' 
     // is parsed as a non-type, but instantiation yields a type"
     CallTemplate<double, A>::ptr MyFunction = &A::max; 
     Average(max, t, v);
    

I do not know where the problem comes from. I have also tried Boost.Function

Community
  • 1
  • 1
Setepenre
  • 1,883
  • 3
  • 13
  • 16
  • What are the a and b arguments to sum() and max() in your two template classes A and B? – Mike Housky Sep 22 '13 at 14:03
  • @Konrad - please restore c++ to the title, as the question really makes no sense without the language specification - it's not just a tag, it's a fundamental part of the meaning. – Chris Stratton Sep 22 '13 at 14:03
  • @ChrisStratton Tags are fundamental part of the meaning of questions, especially the big language ones. They are not "just" anything! – Yakk - Adam Nevraumont Sep 22 '13 at 14:15
  • the sum() max(), A, B example were made up to simplify what I intended to do. nevertheless in the "Original program" my functions take 2 arguments so I kept it that way in the example. I wanted to keep track of these arguments. – Setepenre Sep 22 '13 at 14:16
  • @Setepenre Never mind, I think I get it. Those member functions aren't instance-specific, so they should be declared static. That will simplify a number of things. – Mike Housky Sep 22 '13 at 14:17
  • @Yakk - the tag requires looking in a different place though, which makes the question far less readable. It's barely even on the same page as the title. – Chris Stratton Sep 22 '13 at 14:20
  • @ChrisStratton There are extremely few interfaces that get you to a question on stack overflow that do not display the tag list prominently before you select the question. I guess hitting it via an internet-wide 3rd party search directly? – Yakk - Adam Nevraumont Sep 22 '13 at 14:33
  • @Mike, they are instance specific. A has the data, B just Analyses it. (It was no clear in the example sorry, and the function A has in reality is more complex than "max") – Setepenre Sep 22 '13 at 14:51
  • @ChrisStratton No, that’s *specifically* what tags are for. The title is never meant to stand alone, but in connection with the tags. [Putting tags into the title, unless it forms part of a sentence, is definitely discouraged](http://meta.stackexchange.com/q/103563/1968) – Konrad Rudolph Sep 22 '13 at 15:00
  • You're ignoring how the site actually displays things - the title does stand alone in the layout, and this one is now ambiguous. – Chris Stratton Sep 22 '13 at 15:16

3 Answers3

2

Yes it's possibile.

You're looking for member pointers. The syntax is not obvious however:

struct A
{
    double x, y;
    A(double x, double y) : x(x), y(y) {}
    double sum() { return x + y; }
    double max() { return std::max(x, y); }
};

This is a class that defines a couple of methods (sum and max).

template <typename T>
struct B
{
    std::vector<T> data;
    double average(double (T::*MyMethod)())
    {
        double sum = 0;
        int n = data.size();
        for (int i = 0; i < n; i++)
            sum = sum + (data[i].*MyMethod)();
        return sum / n;
    }
};

This is a class with a method that accepts a method pointer and that will compute the average of the result of calling the pointed method over the elements of a vector.

An example of passing the two A methods is:

int main(int argc, const char *argv[]) {
    B<A> b;
    b.data.push_back(A(1, 2));
    b.data.push_back(A(3, 4));
    b.data.push_back(A(5, 6));
    std::cout << b.average(&A::max) << std::endl;
    std::cout << b.average(&A::sum) << std::endl;
    return 0;
}
6502
  • 112,025
  • 15
  • 165
  • 265
  • I really want to keep classes because i need inheritance. the sum and max example were made up to simplify what I intended to do. Thank you !! – Setepenre Sep 22 '13 at 14:10
0

Solution With Boost.Function

class A
{
    public:
    double sum(double a, double b);
    double max(double a, double b);
}

template <typename T>
class B
{
    std::vector<T> data;

    public:

    double sum (double a, double b);
    double max (double a, double b);
    double average(boost::function<double (T*, double, double) MyFunction, double a, dobule b)
    {
        double sum = 0;
        int n = data.size();

        for ( int i = 0; i < n; i++)
            sum = sum + MyFunction(&data[i], a, b)

            return sum / n;
    }

}

example

boost::function<double (A*, a, b)> MyFunction = &A::max;
average(MyFunction, a, b);

The working example ::

#include <cmath>

using namespace std;

#include <vector>
#include <boost/function.hpp>

#define CallTemplate(returnvalue, FINClass) boost::function<returnvalue (FINClass*, double, double)>

class zc
{
    double n;
    double r;
    double p;

    public:
    zc(double nn, double rr, double pp):n(nn), r(rr), p(pp) {}

    double pv(double t, double v) { return p/ pow ( ( 1 + r + v), ( n - t) );}
    double d(double t, double v)  { return (n - t); }
    double c(double t, double v)  { return (n - t)*(n - t)+(n - t); }
};


template <typename T>
class Master
{
    public:

    Master(){}
    std::vector<T> data;

    double pv(double t, double v) {CallTemplate(double, T) f = &T::pv;  return sum(f, t, v);}
    double d(double t, double v)  {CallTemplate(double, T) f = &T::d;   return weightedAverage(f, t, v);}
    double c(double t, double v)  {CallTemplate(double, T) f = &T::c;   return weightedAverage(f, t, v);}

    double sum(CallTemplate(double, T) MyFunction, double t, double v)
    {
        double sum = 0;
        for( int i = 0, n = data.size(); i < n; i++)
            sum = sum + MyFunction(&data[i], t, v);

        return sum;
    }

    double weightedAverage(CallTemplate(double, T) MyFunction, double t, double v)
    {
        double sum = 0;
        double weight = 0;
        double buf =0;

        for( int i = 0, n = data.size(); i < n; i++)
        {
            buf = data[i].pv(t, v);

            sum = sum + buf;

            weight = weight + MyFunction(&data[i], t, v) * buf;
        }

        return weight/sum;
    }

};

int main()
{
    Master<zc> A;

    for (int i = 1; i < 10; i++)
        A.data.push_back(zc(i, 0.1, 100));

    A.data.push_back(zc(10, 0.1, 1100));


    cout << A.pv(0, 0) << endl;
    cout << A.d(0, 0) << endl;
    cout << A.c(0, 0) << endl;

    return 0;
}
Setepenre
  • 1,883
  • 3
  • 13
  • 16
0

The common C++ idiom to do these kind of things is to pass a function "object" and accept it as a template parameter:

#include <algorithm>

// definition of class A by user http://stackoverflow.com/users/320726/6502
// in answer http://stackoverflow.com/a/18944672/420683
struct A
{
    double x, y;
    A(double x, double y) : x(x), y(y) {}
    double sum() const { return x + y; }
    double max() const { return std::max(x, y); }
};

template <typename T>
struct B
{
    std::vector<T> data;
    template<class Function>
    double average(Function f)
    {
        double sum = 0;
        int n = data.size();
        for (int i = 0; i < n; i++)
            sum = sum + f(data[i]);
        return sum / n;
    }
};

#include <functional>

int main()
{
    B<A> b{ {{1.0, 42.0}, {2.0, 43.0}, {3.0, 44.0}} };
    b.average([](A const& p){ return p.sum(); });
    b.average(std::mem_fn(&A::sum));
}

This is very general approach as it not only accepts function pointers or member function pointers, but any kind of callable object. Using a member function pointer is simple via std::mem_fn.

dyp
  • 38,334
  • 13
  • 112
  • 177