Re: Container for controlpoints (in cartesian coordinates)?



On Aug 3, 1:48 pm, Kaba <n...@xxxxxxxx> wrote:
dila wrote:
Given such a generic container, perhaps provided with a template
parameter "int N", how do you provide a constructor that accepts N of
it's contained types in order to fully initialize the class?

Knot<1> knot( a );
Knot<2> knot( a, b );
Knot<3> knot( a, b, c );
Knot<N> knot( a, b, c, ..., N );

Of course, a single element Knot<1> should not provide a constructor
that accepts more than one element. However, this seems unavoidable.
Likewise, an N element Knot<N> should only allow N values (or possibly
fewer, if defaults can be set internally).

Duplicate the whole Knot class in order to customize the constructor?

I don't know about the original problem. However this one:
You can do it conveniently by using the Curiously recurring template
pattern. It's what I use for my Vector<N, Real>, so that Vector<2,
float> has a constructor (x, y), which is not present with other N.

The idea is the following:

template <int N, typename Real, typename Derived>
class VectorBase<N, Real, Derived>
{
public:
    // Code here all the common functions for all N.
    Derived& operator+=(const Derived& that) const
    {
         // Do your stuff
         return (Derived&)*this;
    }

};

template <int N, typename Real>
class Vector
    : public VectorBase<N, Real, Vector<N, Real> >
{

};

// Specialize the dimensions you want for additional functionality.

template <typename Real>
class Vector<2, Real>
    : public VectorBase<2, Real, Vector<2, Real> >
{
public:
    Vector(const Real& x, const Real& y)
    {
    }

};

--http://kaba.hilvi.org

Hey, that's really awesome.

I had no idea such specialization was possible.

Thanks!

- dila
.



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)
  • Help with functor/template/constructor
    ... Three_To_One which suppresses 1) dimension and 2) function G ... If I do not provide a default constructor in Three_To_Vec, ... template ... double Go(T fnv, int n, double a, double b) ...
    (comp.lang.cpp)
  • 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)