On 23 Jan, 14:27, "Alf P. Steinbach" <al...@start.no> wrote:
* toadwarble:
I hit on a question today which seemed to be incorrect to me in the
version of g++ I'm using (4.1.1)
I have the following
enum rectype_t {
// Stuff....
};
class recordclass {
// Stuff....
};
class base {
// Stuff.....
protected:
void add_record(const recordclass &);
};
class derived1 : public base {
// More stuff....
public:
void add_record(const rectype_t);
};
class derived2: public base {
// More stuff...
public:
void add_record(const rectype_t);
};
However when I define the "add_record" functions for derived1 and
derived2 thus:
void derived1::add_record(const rectype_t rt)
{
recordclass rec(rt);
// Fiddle with rec
add_record(rec);
}
It chokes on the "add_record" which was meant to be a call to the base
class version.
It's OK if I put base:add_record(rec) or if I change the name of the
base class function to "add_rec".
Am I wrong to think that is unambiguous overloading or is g++ wrong?
"add_record" in "derived1" hides the "add_record" in "base".
You can either qualify the call with class name,
base::add_record( rec );
which can alternatively be done via "derived1" wrapper,
void add_record( recordclass const& r ) { base::add_record( r ); }
or call via a "base" reference or pointer,
base& super = *this;
super.add_record (rec );
or bring the "base::add_record" overloads into "derived1" via a "using"
declaration
class derived1: public base
{
protected:
using base::add_record;
public:
void add_record( rectype_t const );
};
The FAQ item discussing this is found at <url:http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.9>.
It doesn't discuss the reason why the language is this way. It's simply
a more or less arbitrary choice of one evil instead of another. With
the current rules, an introduction or removal of an overload in the base
class won't affect which function is called in a derived class, except
when you use the "using" solution where you say OK, fine by me.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Thanks. It seems my example is an easier case to deal with that the
function called and mysterious bugs if warnings weren't turned on.
warnings in he finished code).