473,883 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"using" declaration

Hi,

I'm reading "C++ Coding Standards" by Herb Sutter.
On page 67 there's an example which I don't understand:
---------------------------------
class Base{// ...
virtual void Foo(int);
virtual void Foo(int, int);
void Foo(int, int, int);
};

class Derived : public Base { // ...
virtual void Foo(int); // overrides Base::Foo(int), but hides the others
};

Derived d;
d.Foo(1); // ok
d.Foo(1,2); // error
d.Foo(1,2,3); // error
-----------------------------------

I don't understand why the functions "virtual void Foo(int, int)" and
"void Foo(int, int, int)" are hidden by Derived::Foo(in t) ?
My opinion was that a derived class is always inheriting all functions
of a base class and can additionally override them for a specific purpose
like Foo(int).
To bring the other Base::Foo overloads into scope Sutter is using the
statement

using Base::Foo;

within the Derive class.
Thank you for your answers.

Greeting,
Chris
Jul 23 '05 #1
4 1831
Christian Christmann wrote:

Hi,

I'm reading "C++ Coding Standards" by Herb Sutter.
On page 67 there's an example which I don't understand:
---------------------------------
class Base{// ...
virtual void Foo(int);
virtual void Foo(int, int);
void Foo(int, int, int);
};

class Derived : public Base { // ...
virtual void Foo(int); // overrides Base::Foo(int), but hides the others
};

Derived d;
d.Foo(1); // ok
d.Foo(1,2); // error
d.Foo(1,2,3); // error
-----------------------------------

I don't understand why the functions "virtual void Foo(int, int)" and
"void Foo(int, int, int)" are hidden by Derived::Foo(in t) ?
My opinion was that a derived class is always inheriting all functions
of a base class and can additionally override them for a specific purpose
like Foo(int).
It is a direct consequence of how the compiler looks up a function.
First it searches the class hierarchy for a class which has that function.

In your specific example, when you write
d.foo(1,2);

the compiler looks up the type of d. It is Derived. Thus it looks into class
Derived, if there are one or more functions called Foo(). If there are, proceed
to the next step. If there are no, then look up the base class and try there.
Note: The number and type of the arguments is irrelevant in this step. The compiler
searches for the functions just by looking at their names!

The next step is to decide which function to choose based on the number of arguments
and their types in the set of found functions of the previous step.
In your specific case, the set of those functions contains only 1 function, since Derived
contains only 1 function called 'Foo'. The compiler sees, that Foo takes only one argument
but the call specifies 2 parameters. Thus the error message.
Note: If no argument match can be made, the compiler stops! It doesn't look for other
functions in the base class(es).

That's called hiding: A function in a derived class hides all functions with the same
name in the base class(es).

To bring the other Base::Foo overloads into scope Sutter is using the
statement

using Base::Foo;

within the Derive class.


That's one way to work around function hiding.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #2
* Christian Christmann:
I'm reading "C++ Coding Standards" by Herb Sutter.
On page 67 there's an example which I don't understand:
---------------------------------
class Base{// ...
virtual void Foo(int);
virtual void Foo(int, int);
void Foo(int, int, int);
};

class Derived : public Base { // ...
virtual void Foo(int); // overrides Base::Foo(int), but hides the others
};

Derived d;
d.Foo(1); // ok
d.Foo(1,2); // error
d.Foo(1,2,3); // error
-----------------------------------

I don't understand why the functions "virtual void Foo(int, int)" and
"void Foo(int, int, int)" are hidden by Derived::Foo(in t) ?


It's a more-or-less arbitrary language design decision; there are arguments
for, and arguments against.

The hiding ensures that calls to Foo in Derived won't inadvertently become
bound to inherited Foo implementations ; in particular, with the hiding in
effect, which implementation a call to Foo in Derived is bound to cannot be
changed by adding new functions to Base (oops, I added a Base function and
suddenly Derived stopped working), part of the "fragile base class" problem.

This gives control to the Derived programmer, but it also means that the
"Derived is a kind of Base" rule is broken. I think the AKO rule is _much_
more important than "fragile base class", and the hiding is at best only a
very partial solution to that, and nobody finds the hiding very intuitive
(well, nobody I know). But then that's how it is with a lot of things.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
Christian Christmann wrote:

I don't understand why the functions "virtual void Foo(int, int)" and
"void Foo(int, int, int)" are hidden by Derived::Foo(in t) ?
My opinion was that a derived class is always inheriting all functions
of a base class and can additionally override them for a specific purpose
like Foo(int).


I'm afraid I don't know why it was done this way, but it is indeed the
case. Section 13.2/1 has this example:

class B{
public:
int f(int);
};

class D: public B{
public:
int f(char*);
};

Here D::f(char*) hides B::f(int) rather than overloading it.

void h(D* pd)
{
pd->f(1); // error: D::f(char*) hides B::f(int)
pd->B::f(1); // OK
pd->f("Ben"); // OK, calls D::f
}

- end example.

Paragraph 10.2/2 is probably relevant, but I couldn't quite decipher it.

Regards,
Jacques.
Jul 23 '05 #4

"Christian Christmann" <pl*****@yahoo. de> wrote in message
news:42******** *************** @newsread4.arco r-online.net...
Hi,

I'm reading "C++ Coding Standards" by Herb Sutter.
On page 67 there's an example which I don't understand:
---------------------------------
class Base{// ...
virtual void Foo(int);
virtual void Foo(int, int);
void Foo(int, int, int);
};

class Derived : public Base { // ...
virtual void Foo(int); // overrides Base::Foo(int), but hides the others
};

Derived d;
d.Foo(1); // ok
d.Foo(1,2); // error
d.Foo(1,2,3); // error
-----------------------------------

I don't understand why the functions "virtual void Foo(int, int)" and
"void Foo(int, int, int)" are hidden by Derived::Foo(in t) ?
My opinion was that a derived class is always inheriting all functions
of a base class and can additionally override them for a specific purpose
like Foo(int).
To bring the other Base::Foo overloads into scope Sutter is using the
statement

using Base::Foo;

within the Derive class.
Thank you for your answers.

Greeting,
Chris


Regarding the issue of function hiding it does not really matter whether the
function is declared virtual or not. See section 23.7 of the FAQ.

HTH
Chris
Jul 23 '05 #5

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

Similar topics

2
6411
by: Jacek Dziedzic | last post by:
Is it valid to use a "using namespace foo" (as opposed to using foo::bar which I'm sure is legal) within a class declaration? My compiler rejects it, but I've been told it's valid. Can anyone please confirm or deny? TIA, - J.
17
3529
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example using std::cin; using std::cout;
8
4380
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could somebody explain it to me? I am using MSVC 6.0. Thanks a lot,
14
2185
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type too: using std::vector<int>; Both seem to "work" on the compiler I have and I can't find any documentation saying which is correct, or are both correct?
5
3603
by: Jacky Yuk | last post by:
Hi all, I am new to c++ but using c for long time. Recently, I created a MFC GUI project by VC/C++ 6.0. Everything was fine until I wanted to use "template": template <typename T> class AutoComPtr { ... The following errors were shown:
10
1609
by: Andreas Müller | last post by:
Hi all, I have a class SwitchCursor, that implements IDisposable. Is it legal to use an object that is not assigned to a reference inside a using statement like this: using(new SwitchCursor())// is the SwitchCursor safe from GC? { }
8
1486
by: Geri Reshef | last post by:
Many times I find code examples in the internet which don't have the "using" statements needed to run them, and without it the compilation fails. Is there a way to find the correct "using" statement given the variable declaration?
30
4138
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
12
2418
by: Steve Pope | last post by:
Compiling the following works on my system: file main.cpp: #include <iostream> namespace space { int foo; }
25
2603
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For example: with quitCommandButton .enabled = true .default = true end with
0
9936
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11138
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10412
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7123
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5990
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4215
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3231
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.