Re: Container for controlpoints (in cartesian coordinates)?
- From: dila <dilabox@xxxxxxxxx>
- Date: Sun, 3 Aug 2008 06:25:08 -0700 (PDT)
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
.
- References:
- Prev by Date: Re: Container for controlpoints (in cartesian coordinates)?
- Next by Date: Mesh Generation using Media Axis
- Previous by thread: Re: Container for controlpoints (in cartesian coordinates)?
- Next by thread: Offset Curves
- Index(es):
Relevant Pages
|