473,320 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Virtual/Non-Virtual + different signature

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
4 2481
"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
"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.