|
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"?
|
|
|
|