473,587 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Standard C++ design question--- geometry class

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.pus h_back( new Point( v )); }
};
// bezier curve
class Bezier : public Curve
{
public:
void appendPt( const Vec3f &v )
{ _controlPts.pus h_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<Be zierPoint*>(bez ierCurve.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
Jul 19 '05 #1
5 4537
"Jon" <in******@mit.e du> 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<Be zierPoint*>(bez ierCurve.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
Jul 19 '05 #2
> 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
Jul 19 '05 #3
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?

Thanks,
Jon
Jul 19 '05 #4
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
Jul 19 '05 #5
> > 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
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
1924
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:
1
2032
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 to kick off the process and continue to work in the appliaction while getting progress updates and the ability to cancel. The method that seems...
18
2046
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 the following design question. Obviously you need a stream to write the log file. Should this stream be created every time the log needs to be...
6
2109
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 responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
5
1728
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.). Its very primitive, very simple and kind of gets the job done. The only drawback, I find myself littering my entire code base like this:
7
308
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 (CAppInit) that I use for application configuration. It behaves similarly to Configuration.AppSettings with some extra functionality. This class is...
4
2042
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 other things. What would be a good design to accomplish these goals? Here are the ideas that I have come up with: Class X = class with data that I...
29
2208
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 "new" contract. I have a method in my contract class called "Save" which is called like this... dim oContract as new Contract
0
7920
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.