Wednesday, August 12, 2009

More on operator overloading

So this whole problem led me to think about some other issues.


class Box
{
bool
contains
( double const & location
) const;
};


so someone would call:


Box box(....);
if (box.contains(location))
{


In c++ the "contains" could be extracted from the class, but that would result in this call:


Box box(...);
if (contains(box, location))
{


This is starting to become less clear

And now we break from c++


if (box contains(location)) // or ( box contains location )
{


Examining the first class method example, a c++ compiler would roughtly translate this into something like

Box::contains(pointer to instance box, location);

while the second would resolve to calling

contains(box, location);

Operator overloading

Today I was busy trying to extend a spatial index class to arbitrary dimensions.
As I was looking at this the implications were huge. We have this code base that contains these classes like Vector2D, Vector3D, Box1D, Box2D, Stats1D, Stats3D, etc. Not a very good match for something like SpatialIndexD<4>

I was thinking then about std::array and how this might affect the design of everything above.

So everyone knows about C++ operator overloading. ++, /, *, +=, etc. These all work fine for what they do.

Looking at something like Vector2D/Vector3D I see operations like "dot" and "cross" and "wedge". Still operators but they just don't have a "blessed" symbol associated with them.

So of course I write a test program


double operator dot(double const & lhs, double const & rhs) { return lhs * rhs; }
int main()
{
std::cout << (5. dot 6.) << std::endl;
return 0;
}


And of course....no compilation.