Click here to Skip to main content
14,829,399 members

Mathpurist - Professional Profile



Summary

    Blog RSS
1
Debator
1
Enquirer
251
Participant
0
Author
0
Authority
0
Editor
0
Organiser
No Biography provided

Reputation

Weekly Data. Recent events may not appear immediately. For information on Reputation please see the FAQ.

Privileges

Members need to achieve at least one of the given member levels in the given reputation categories in order to perform a given action. For example, to store personal files in your account area you will need to achieve Platinum level in either the Author or Authority category. The "If Owner" column means that owners of an item automatically have the privilege. The member types column lists member types who gain the privilege regardless of their reputation level.

ActionAuthorAuthorityDebatorEditorEnquirerOrganiserParticipantIf OwnerMember Types
Have no restrictions on voting frequencysilversilversilversilver
Bypass spam checks when posting contentsilversilversilversilversilversilvergoldSubEditor, Mentor, Protector, Editor
Store personal files in your account areaplatinumplatinumSubEditor, Editor
Have live hyperlinks in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Have the ability to include a biography in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Edit a Question in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Edit an Answer in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Delete a Question in Q&AYesSubEditor, Protector, Editor
Delete an Answer in Q&AYesSubEditor, Protector, Editor
Report an ArticlesilversilversilversilverSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending ArticlegoldgoldgoldgoldSubEditor, Mentor, Protector, Editor
Edit other members' articlesSubEditor, Protector, Editor
Create an article without requiring moderationplatinumSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending QuestionProtector
Approve/Disapprove a pending AnswerProtector
Report a forum messagesilversilverbronzeProtector, Editor
Approve/Disapprove a pending Forum MessageProtector
Create a new tagsilversilversilversilver
Modify a tagsilversilversilversilver

Actions with a green tick can be performed by this member.


 
QuestionPolymorphic operator overloading Pin
Mathpurist21-Jul-06 11:34
MemberMathpurist21-Jul-06 11:34 
I have Visual C++, Version 6.0, on Windows 98. Old, I know, but I will purchase a new one based on what I learn about its ability for polymorphic overloading of operators.

I want to overload operators in a polymorphic fashion. The idea is to be able to create a sort of algebra of geometric objects. So, for example, I can easily overload vector operators for vector addition by overloading the + operator for that particular class, and the code will look like mathematical notation.

If however I want to overload operators for a class of objects, I must use a messy construction, depending on dereferencing pointers. The notation doesn't look quite like textbook notation.

This is because Microsoft C++ does polymorphism through pointers, not through the objects themselves.

I will explain by example:

I have two classes of objects: spline and cube. Both are based on class geom. I also have a matrix class. Code snippets follow:

class geom : public CObject {
public:
geom();
virtual ~geom();
DECLARE_SERIAL(geom);
void Serialize(CArchive& ar);
geom(geom&);
void operator=(geom& obj);
virtual geom operator *(matrix); // Right product by matrix
virtual geom operator*=(matrix&); // Right product by matrix
}; // class geom

class cube : public geom {
public:
cube();
virtual ~cube();
DECLARE_SERIAL(cube);
void Serialize(CArchive& ar);
geom operator *(matrix); // Right product by matrix
geom operator*=(matrix&); // Right product by matrix
// A cube has some vectors in it, which can be transformed by operator *
// overloaded for matrix multiplication
}; // class cube

class spline : public geom {
public:
spline ();
virtual ~ spline ();
geom operator *(matrix); // Right product by matrix
geom operator*=(matrix&); // Right product by matrix
// A spline has some vectors in it, which can be transformed by operator *
// overloaded for matrix multiplication
}; // class spline


I want to use matrix multiplication thusly:

spline sp;
spline sp_transformed;
cube cu;
cube cu_transformed;
matrix xformer;

sp_transformed= sp * xformer; // Comes out not really a spline- only a geom.
cu_transformed= cu * xformer; // Comes out not really a cube- only a geom.

What comes out is just the geom part. No amount of doodling with
geom(geom&);
void operator=(geom& obj);
will help.

However, if I use pointers, as follows, this works. But it is clumsy, and I also have to
worry about deleting the intermediate products:

class geom : public CObject {
public:
geom();
virtual ~geom();
DECLARE_SERIAL(geom);
void Serialize(CArchive& ar);
geom(geom&);
void operator=(geom& obj);
virtual geom *operator *(matrix); // Right product by matrix
virtual geom *operator*=(matrix&); // Right product by matrix
}; // class geom

class cube : public geom {
public:
cube();
virtual ~cube();
DECLARE_SERIAL(cube);
void Serialize(CArchive& ar);
geom *operator *(matrix); // Right product by matrix
geom *operator*=(matrix&); // Right product by matrix
// A cube has some vectors in it, which can be transformed by operator *
// overloaded for matrix multiplication
}; // class cube

class spline : public geom {
public:
spline ();
virtual ~ spline ();
geom *operator *(matrix); // Right product by matrix
geom *operator*=(matrix&); // Right product by matrix
// A spline has some vectors in it, which can be transformed by operator *
// overloaded for matrix multiplication
}; // class spline

spline sp;
spline sp_transformed;
cube cu;
cube cu_transformed;
matrix xformer;

sp_transformed= *(sp * xformer); // Messy
cu_transformed= *(cu * xformer); // Messy

So, my question is, does anybody know of a C++ implementation which supports the first kind of polymorphic overloading, which I might call "puristic"?




General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.