By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

Virtual/Non-Virtual + different signature

P: n/a
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!
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"garyolsen" <ga*******@yahoo.com> wrote in message
news:1T***********************@bgtnsc04-news.ops.worldnet.att.net...
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

Jul 22 '05 #2

P: n/a
"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

Jul 22 '05 #3

P: n/a

"garyolsen" <ga*******@yahoo.com> wrote in message
news:1T***********************@bgtnsc04-news.ops.worldnet.att.net...
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.
Jul 22 '05 #4

P: n/a

"David White" <no@email.provided> wrote in message
news:FG*****************@nasal.pacific.net.au...

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.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.