J
Jon
Hello all,
I'm certain this question has been asked before; I was unsure what
terms to search for within the newsgroup archive to find the proper
answer I wanted. Hopefully someone can point me to some relevant
postings or websites.
In any case, I want to create a geometry class that has some base
functionalities. The problem I am particularly interested in is such:
(the classes are empty for clarity of problem; assume standard
constructors and (virtual) destructors exist; and for protected or
private class members, standard get() and set() methods exist)
class Vec3; // 3-element vector
class Geometry {};
class Point : public Geometry
{
public:
Point( const Vec3f &v ) : Geometry(), _vertex(v) {}
protected:
Vec3 _vertex; // 3D location of point
};
class Curve : public Geometry
{
public:
virtual void appendPt( const Vec3f& ) = 0;
protected:
vector<Point*> _controlPts;
};
class Surface : public Geometry {};
class Volume : public Geometry {};
class BezierPoint : public Point
{
public:
BezierPoint( const Vec3f &v ) :
Point(v), _frontHandle(v), _backHandle(v)
{}
protected:
/// these members are used to determine shape of the curve
Vec3 _frontHandle; // 3D location of "front" handle
Vec3 _backHandle; // 3D location of "back" handle
}
// a connected series of lines
class Polyline : public Curve
{
public:
void appendPt( const Vec3f &v )
{ _controlPts.push_back( new Point( v )); }
};
// bezier curve
class Bezier : public Curve
{
public:
void appendPt( const Vec3f &v )
{ _controlPts.push_back( new BezierPoint( v )); }
};
Now, the problem:
I want to add iterator methods that return pointers to the proper
Point object, but I want the returned object to be the correct type:
in other words, if I had a begin() method that returns the first point
in the curve, Polyline::begin() should return a Point* and
Bezier::begin() return a BezierPoint*.
At the same time, I don't want to have to explicitly cast any given
Curve into the proper type before calling begin().
Is there a way to do this?
I initially thought Curve should have a pure virtual function Point*
begin() = 0, but then subclasses (namely Bezier) can't override the
return type.
The solution I'm using now is have Curve contain all the iterator
methods and always returning Point pointers, but when I deal with
bezier curves I hate constantly having to do
dynamic_cast<BezierPoint*>(bezierCurve.begin()), especially when I
want to iterate through all the points to draw the curve, for example.
I hope this made sense. I would appreciate any feedback.
Regards,
Jon
I'm certain this question has been asked before; I was unsure what
terms to search for within the newsgroup archive to find the proper
answer I wanted. Hopefully someone can point me to some relevant
postings or websites.
In any case, I want to create a geometry class that has some base
functionalities. The problem I am particularly interested in is such:
(the classes are empty for clarity of problem; assume standard
constructors and (virtual) destructors exist; and for protected or
private class members, standard get() and set() methods exist)
class Vec3; // 3-element vector
class Geometry {};
class Point : public Geometry
{
public:
Point( const Vec3f &v ) : Geometry(), _vertex(v) {}
protected:
Vec3 _vertex; // 3D location of point
};
class Curve : public Geometry
{
public:
virtual void appendPt( const Vec3f& ) = 0;
protected:
vector<Point*> _controlPts;
};
class Surface : public Geometry {};
class Volume : public Geometry {};
class BezierPoint : public Point
{
public:
BezierPoint( const Vec3f &v ) :
Point(v), _frontHandle(v), _backHandle(v)
{}
protected:
/// these members are used to determine shape of the curve
Vec3 _frontHandle; // 3D location of "front" handle
Vec3 _backHandle; // 3D location of "back" handle
}
// a connected series of lines
class Polyline : public Curve
{
public:
void appendPt( const Vec3f &v )
{ _controlPts.push_back( new Point( v )); }
};
// bezier curve
class Bezier : public Curve
{
public:
void appendPt( const Vec3f &v )
{ _controlPts.push_back( new BezierPoint( v )); }
};
Now, the problem:
I want to add iterator methods that return pointers to the proper
Point object, but I want the returned object to be the correct type:
in other words, if I had a begin() method that returns the first point
in the curve, Polyline::begin() should return a Point* and
Bezier::begin() return a BezierPoint*.
At the same time, I don't want to have to explicitly cast any given
Curve into the proper type before calling begin().
Is there a way to do this?
I initially thought Curve should have a pure virtual function Point*
begin() = 0, but then subclasses (namely Bezier) can't override the
return type.
The solution I'm using now is have Curve contain all the iterator
methods and always returning Point pointers, but when I deal with
bezier curves I hate constantly having to do
dynamic_cast<BezierPoint*>(bezierCurve.begin()), especially when I
want to iterate through all the points to draw the curve, for example.
I hope this made sense. I would appreciate any feedback.
Regards,
Jon