473,396 Members | 2,121 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,396 software developers and data experts.

Still a bit confused


A few people have been asking about variants of this recently, and I'm
still not totally clear.

A class Base declares, amongst other things, a method called func().
Because derived classes might replace this with their own
implementation, func is declared as virtual in Base:

// header
class Base
{
public:
virtual int func(int arg1);
}

Because not all derived classes will wish to provide their own
implementation, Base provides a default one:

int
Base::func(int arg1)
{
......
}

This particular derived class in fact does not replace func():

class Derived : public Base
{
......
}

In the application code, I create a Derived object, and then call the
func() method:

{
Derived x;

int i = x.func(12345);
}

So, by my understanding, this is all fine, and indeed, testing it
works fine; the Base::func() is called exactly as I would expect, and
all is exactly as written in the FAQ-Lite Q20.6[1]. And If Derived
does declare a replacement with the same signature, then again, it all
works as expected, and the Derived version is called. But as soon as
Derived declares a func() member of its own, but with a totally
different signature, the Base version is hidden, and we have to have a
'using' clause in Derived to make it visible again. This is where I'm
stuck; surely, it should look up by the signature, then find
Base::func(int) through the vtable? Why does the damned thing get
hidden?

1.
http://www.ensta.fr/~diam/c++/online....html#faq-20.6
--
PGP key ID 0xEB7180EC
Nov 1 '07 #1
3 1310
Keith Willis wrote:
[..Base declares virtual 'func()'..]
as soon as
Derived declares a func() member of its own, but with a totally
different signature, the Base version is hidden, and we have to have a
'using' clause in Derived to make it visible again. This is where I'm
stuck; surely, it should look up by the signature, then find
Base::func(int) through the vtable? Why does the damned thing get
hidden?
The lookup is NOT by the signature. The lookup is by the *name* only.
Does this help?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 1 '07 #2
My understanding is that it is done this way to prevent horrible bugs
that could show up when implicit type promotion/etc is involved.

For example, if you have Base::func(double x), and Derived::func(int
y) and you have something like:

Derived obj();
obj.func(1);

Which function is called? Which did you intend to call? It might be
easy to call the specific one you need in simple cases, but if you are
using a 3nd party library with deep levels of inheritance and you
derive some classes, you might not be aware of the existence of a
member function with the same name but different signature (and the
two functions could do totally different things, or worse, similar
ones with side-effects).

So, the decision to hide the base's function is done to protect the
programmer from such situations. If you use the "using" keyword, you
are declaring to the compiler that you are aware of the name clash,
and that you take responsibility for any implicit conversions.

Nov 1 '07 #3
On Thu, 01 Nov 2007 07:40:57 -0700, Ioannis Gyftos
<io************@gmail.comwrote:
>My understanding is that it is done this way to prevent horrible bugs
that could show up when implicit type promotion/etc is involved.
<snip>
>So, the decision to hide the base's function is done to protect the
programmer from such situations. If you use the "using" keyword, you
are declaring to the compiler that you are aware of the name clash,
and that you take responsibility for any implicit conversions.
Ahhhhhh! Now THAT actually makes sense.
Thanks you for the clarification.
--
PGP key ID 0xEB7180EC
Nov 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Neil Greenough | last post by:
I'm still confused as to how the language used in .asp looks up certain information. In my case, I have a Access Database, and I want .asp to give me an estimation online, using the information...
102
by: RFox | last post by:
I date back to the early days of the web when HTML was limited but very managable, and have always maintained that hand-coding HTML gives you far better control and cleaner HTML markup than any...
687
by: cody | last post by:
no this is no trollposting and please don't get it wrong but iam very curious why people still use C instead of other languages especially C++. i heard people say C++ is slower than C but i can't...
9
by: JohnSmith | last post by:
I suspect this is easy, but I have been stumped for a day trying to solve this.. I want to be able to have an unlimited number of aspx pages that all use the code in one class file. I want code...
3
by: frekster | last post by:
All. I have a folder/files that I have added asp.net to have read/write/etc. privlidges via the properties of the folder/files and security tab. However, when I run my asp.net page, I still get...
7
by: D. Patrick | last post by:
I need to connect to an Oracle database. I've never done it before. I see that with framework 1.1 you had to download ODP.NET from Oracle's site in order for the framework classes to even work....
6
by: pookiebearbottom | last post by:
Let's say I have headers Sal.h with this class class Sal { public: int doit() { return 1;} }; now I know that the compilier can choose NOT to inline this function. So if I include this in...
12
by: Stef Mientki | last post by:
In the example below, "pin" is an object with a number of properties. Now I want 1- an easy way to create objects that contains a number of these "pin" 2- an multiple way to access these "pin",...
2
by: Peter | last post by:
Hi, I have a problem with Listview using checkboxes. If i check items by code BEFORE the form is shown the Listview.Items are confused during the ItemChecked Event !!! After showing the...
0
by: George2 | last post by:
Hello everyone, Sorry that this question is related to another question I posted some time before because I have some new findings and self-analysis. My question is why sometimes from perfmon...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.