"David White" <no@email.provided> wrote in message
news:gM*****************@nasal.pacific.net.au...
class base{
public:
void NonVirtualMethod(int a);
virtual void VirtualMethod(int a);
};
class derived{
I missed this:
class derived : public base {
public:
void NonVirtualMethod(int a, char c);
void VirtualMethod(int a, char c);
};
In the above sample code, I have some questions:
1. Is it good coding to have different signatures for a non-virtual methods in base and derived classes?
I assume you mean methods (i.e., member functions) that have the same
name. Well, it's a matter of opinion. Mine is that it causes name-hiding
headaches, so I'd prefer to avoid it, but sometimes you need to do it.
2. Is it good coding to have different signatures for a virtual methods
in base and derived classes?
I don't like that either, for the same reason.
[snip]
4. When should/shouldn't use either of such coding?
It's up to you.
To expand on this a little, there are times when you might need to add a
member to a derived class that is clearly "overloading" a member of the base
class, or is clearly an equal member of a family of overloads in the base
class, as far as clients are concerned. In these circumstances the case for
using the same name in the derived class is compelling, IMO. Clients should
not have to care which class a given overload is in, or have to use
different member names for different, seemingly arbitrarily chosen, sets of
overloads of the same service.
In hindsight, I withdraw my answers to 1 & 2 above. I suggest using the same
name if it's the most appropriate name to use, whether it hides a name or
not.
BTW, you can unhide your base-class names as follows:
class derived : public base {
public:
using base::NonVirtualMethod;
void NonVirtualMethod(int a, char c);
using base::VirtualMethod;
void VirtualMethod(int a, char c);
};
DW