Help with functor/template/constructor

From: jack (idlor_at_yahoo.com)
Date: 02/23/04


Date: 23 Feb 2004 14:40:40 -0800

Hi there,

I have a function F(x, y, z)
and I want to calculate f(a) + f(b), where f(x) = F(x, x, z0)
z0 is fixed.

Suppose somebody wrote a routine called "Compute" which simply
computes f(a) + f(b) for any one dimensional function f.

Then I create a f1dim which converts any n-dimensional function
G to a one dimensional f(x) = G(x, x, ..., x), via a class
Three_To_One which suppresses (fixes) 1) dimension and 2) function G
in f's argument list.

Also create a class Three_To_Vec which fixes parameter z
in my origional function F, and make it look like a function
with a single array/pointer parameter, i.e., H(w[]) = F(w[0], w[1], z0).

1) If I do not provide a default constructor in Three_To_Vec,
   the program won't compile. Why?

2) Why are the destructors of both classes (Three_To_One and Three_To_Vec)
   called multiple times, while the constructors are only called once?

Thank you for your help.

Jack

========================== source code in a single file ==================

#include <iostream>
using namespace std;

/* ================= Original function ================== */

double my_3d_f(double x, double y, double z)
{
        return x*x*x + 2.0*x*y + 3.0*x*z + x + 3.0*z + 6.0;
}

/* ================= Two Classes used in Functor ============= */

template <class T>
class Three_To_One
{
public:
        double (*m_f)(double, int, T);
        int m_p1;
        T m_p2;

        Three_To_One(double(*f)(double, int, T), int p1, T p2)
        {
                cout << " 3-to-1: c-tor" << endl;
                m_f=f;
                m_p1=p1;
                m_p2=p2;
        }

        ~Three_To_One() { cout << " 3-to-1: D-tor" << endl; }

        double operator()(double x)
        {
                return m_f(x, m_p1, m_p2);
        }
};

class Three_To_Vec
{
public:
        double (*m_f)(double, double, double);
        double m_p;

        Three_To_Vec()
        {
                cout << "3-to-V: default" << endl;
        }

        Three_To_Vec(double(*f)(double, double, double), double p)
        {
                cout << "3-to-V: c-tor" << endl;
                m_f = f;
                m_p = p;
        }

        ~Three_To_Vec() { cout << "3-to-V: D-tor" << endl; }

        double operator()(double theta[])
        {
                return m_f(theta[0], theta[1], m_p);
        }
};

/* ========== Somebody's function to compute f(a) + f(b) =================== */

template <class T>
double Compute(T f, double a, double b)
{
        return f(a) + f(b);
}

/* ========== Convert n-dimensional to 1-dimensional ================= */

template <class T>
double f1dim(double t, int n, T fnd)
{
        int j;
        double fval, *x;

        x = new double[n];
        for(j=0; j < n; ++j)
                x[j] = t;
        fval = fnd(x);
        delete []x;
        return fval;
}

/* =========== My function to compute F(a,a,z0) + F(b, b, z0) =================== */

template <class T>
double Go(T fnv, int n, double a, double b)
{
        return Compute(Three_To_One<T>(f1dim, n, fnv), a, b);
}

int main()
{

        double result;
        
        result = Go(Three_To_Vec(my_3d_f, 3.14159), 3, 2.0, 9.0);
        // compute F(2, 2, pi) + F(9, 9, pi)

        cout << "result = " << result << endl;

        return 0;
        
}



Relevant Pages

  • friend ostream& operator<< (ostream&, Array<T>&);
    ... Array(int itsSize = DefaultSize); ... int GetSize() const ... make sure the function template has already been declared and add after ... // implement the Constructor ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Container for controlpoints (in cartesian coordinates)?
    ... Knot<3> knot; ... You can do it conveniently by using the Curiously recurring template ... float> has a constructor, which is not present with other N. ... template <int N, typename Real, typename Derived> ...
    (comp.graphics.algorithms)
  • Re: Overloading << operator
    ... template ... Array(int itsSize = DefaultSize); ... int GetSize() const ... // implement the Constructor ...
    (microsoft.public.vc.language)
  • Re: Container for controlpoints (in cartesian coordinates)?
    ... Knot<2> knot; ... a single element Knotshould not provide a constructor ... You can do it conveniently by using the Curiously recurring template ... template <int N, typename Real, typename Derived> ...
    (comp.graphics.algorithms)
  • Simple script annimation problem usig swing
    ... To change this template, ... public void destroy{ ... public state cstate; ... public static int respawn = 20; ...
    (comp.lang.java.programmer)