Re: Overriding overloaded member functions
Julian Hsiao wrote:
Hi,
Suppose I have a base class, defined thusly:
class Rect { /* ... */ } ;
class Point { /* ... */ };
class RegularPoly
{
public:
virtual ~RegularPoly() { }
virtual void define(Point center, int radius, int edges)
{
// ...
}
virtual void define(Point center, Point *vertexes)
{
int radius;
int edges;
// Compute the correct radius, edge based on the vertexes,
// error checking, etc.
define(center, radius, edges);
}
virtual void define(Rect bound, int edges)
{
Point center;
int radius;
// Compute the regular polygon inscribed by bound
define(center, radius, edges);
}
// More overloaded define() ...
};
Note that RegularPoly::define(Point, int, int) is the actual function that
does the defining, and all the other overloaded forms call it.
Now suppose I want a regular polygon whose edges are painted in different
colors, say it alternates between red and green:
class AltColorRegularPoly : public RegularPoly
{
public:
virtual ~AltColorRegularPoly() { }
using RegularPoly::define;
virtual void define(Point center, int radius, int edges)
{
// Make the edges alternate between red and green
RegularPoly::define(center, radius, edges);
}
};
This works out nicely, but only because I know which of the overloaded
functions is the "master" master function. In general I wouldn't know that
(unless it's documented), so I'd be forced to override all of them. The
burden would only get worse as the inheritance hierarchy deepens. Moreover,
such dependency could arise could arise with non-overloaded member functions
as well.
Other than relying on documentation, is there an idiomatic way of marking the
"master" function, so that others writing derived classes knows which to
override?
If other define() functions are nothing more complicated than a wrapper
for the "master" define() then the optimizer should be able to inline
the call (and therefore better context analysis can be done.)
On the other hand, if you really want to mark the "master" function
clearly why not just give it another name and keep it protected (and
non-virtual)?
Thanks.
Julian Hsiao
My spam filter's lame...
Regards,
Ben
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]