Virtual/Non-Virtual + different signature

G

garyolsen

class base{
public:
void NonVirtualMethod(int a);
virtual void VirtualMethod(int a);
};

class derived{
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?

2. Is it good coding to have different signatures for a virtual methods in
base and derived classes?

3. Is the coding in case 1 so-called "hiding"?

4. When should/shouldn't use either of such coding?

Many thanks!
 
D

David White

garyolsen said:
class base{
public:
void NonVirtualMethod(int a);
virtual void VirtualMethod(int a);
};

class derived{
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.
3. Is the coding in case 1 so-called "hiding"?

Yes. So is case 2.
4. When should/shouldn't use either of such coding?

It's up to you. Name hiding is an annoyance, but not one that you can't put
up with. If you do have to do it, you can make the base-class signatures
visible in the derived class so that clients of the classes are not
inconvenienced.

DW
 
D

David White

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
 
J

jeffc

garyolsen said:
class base{
public:
void NonVirtualMethod(int a);
virtual void VirtualMethod(int a);
};

class derived{
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?

Unless you have some very specific problem in mind, no - that is considered
bad programming.
2. Is it good coding to have different signatures for a virtual methods in
base and derived classes?
Likewise.

3. Is the coding in case 1 so-called "hiding"?
Yes.

4. When should/shouldn't use either of such coding?

Unless you have a very specific problem that can't be solved in a better
way, or a very specific design that makes hiding valid, then don't do that.
 
J

jeffc

David White said:
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);
};

Yes, I should have added something like this. It's OK to use the same name
in the subclass with a different signature as long as you do this or
redefine the "hidden" functions as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top