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 5 4524
"Jon" <in******@mit.edu> wrote... [...] 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 Geometry {};
class Point : public Geometry {
[...] };
class Curve : public Geometry {
[...]
vector<Point*> _controlPts; };
class BezierPoint : public Point {
[...] }
// a connected series of lines class Polyline : public Curve {
[...] }; // bezier curve class Bezier : public Curve {
[...] };
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?
If the Bezier's container of points will really contain pointers to
'BezierPoint', you don't need to do anything, the pointers from the
vector will be pointers to the objects you created, and if you create
BezierPoints for a Bezier object to insert into the vector, the same
objects will be returned.
I initially thought Curve should have a pure virtual function Point* begin() = 0, but then subclasses (namely Bezier) can't override the return type.
There is no need.
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.
You don't need to. Just make sure 'Point' has all functions necessary
for you to obtain coordinates, etc., to draw the curve.
Victor
> 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?
There _are_ covariant return types in C++ :
class Point {};
class BezierPoint : public Point {};
class Curve
{
public:
virtual Point* begin();
};
class Polyline : public Curve
{
public:
Point* begin();
};
class Bezier : public Curve
{
public:
BezierPoint* begin();
};
As long as the return types are covariant (that is, in the same
hierarchy), it runs fine.
Jonathan
> There _are_ covariant return types in C++ :
This is the answer I was looking for, and I thought C++ had this
feature.
BUT when I try to compile (I'm using Microsoft Visual Studio's nmake
and cl) I get an error like:
..\Bezier.h(78) : error C2555: 'Bezier::begin' : overriding virtual
function differs from 'Curve::begin' only by return type or calling
convention
Am I missing something?
Thanks,
Jon
my apologies for filling your inboxes with my last reply (and this one
too).
i did some quick research in the archives---i didn't realize the term
was "covariance". in any case, i found out it's not my fault but
microsoft's. grr.
thanks,
jon
> > There _are_ covariant return types in C++ : This is the answer I was looking for, and I thought C++ had this feature.
BUT when I try to compile (I'm using Microsoft Visual Studio's nmake and cl) I get an error like:
.\Bezier.h(78) : error C2555: 'Bezier::begin' : overriding virtual function differs from 'Curve::begin' only by return type or calling convention
Am I missing something?
No, vc++ is.
Jonathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Uwe Mayer |
last post by:
Hi,
I have the following inter-class relationships:
__main__: (in file LMCMain.py)
imports module FileIO
defines class LMCMain
instanciats main = LMCMain(...)
FileIO.py:
|
by: dixp |
last post by:
I'm new to writing multithreaded apps and I have a design question. I
have a winforms app and a class which has a method that does processing
which is time intensive. I want the user to be able...
|
by: Nick Z. |
last post by:
I am writing a reusable class for logging.
My goal is to make it as fast and as robust as possible, while keeping
memory usage to the lowest. All members are static.
For some reason I'm stuck on...
|
by: rodchar |
last post by:
Hey all,
I'm trying to understand Master/Detail concepts in VB.NET. If I do a data
adapter fill for both customer and orders from Northwind where should that
dataset live?
What client is...
|
by: Dilip |
last post by:
I have a tiny design question to ask.
I am stuck with a home-brewed logging library that has different
methods for logging messages based on severity (like warning, error,
information etc.). ...
|
by: Steve Long |
last post by:
Hello,
I have a design question that I'm hoping someone can chime in on. (I still
using VS 2003 .NET 1.1 as our company has not upgraded XP to sp2 yet. duh I
know, I know)
I have a class I wrote...
|
by: alacrite |
last post by:
I have a class that I want to turn its contents into csv file. I want
to be able to set the value of the delimiter, the name of the file it
gets saved to, the path of that file, and maybe a few...
|
by: Brad Pears |
last post by:
Here is a simple OO design question...
I have a Contract class. The user can either save an existing contract or
they start off fresh with a blank contract, fill in the data and then save a...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |