class A {
public:
virtual void foo(int b) {}
virtual void foo(char* b) {}
};
class B : public A{
virtual void foo(int b) {}
};
void
foo()
{
B* b;
b->foo("tgre");
}
this code does not compile, neither in Visual C++ nor in Codewarrior.
Does someone know why ?
Aymeric Bard. 9 3536
"AGoTH" <ag***@virtools .com> wrote in message
news:26******** *************** ***@posting.goo gle.com... class A { public: virtual void foo(int b) {} virtual void foo(char* b) {} };
class B : public A{ virtual void foo(int b) {} };
void foo() { B* b; b->foo("tgre"); }
this code does not compile, neither in Visual C++ nor in Codewarrior. Does someone know why ?
The 'foo(int)' in B hides 'foo(char*)' of A and overloads 'foo(int)'
Define an overload for 'foo(char*)' in B, where you just forward the call to
'A::foo(int)'.
hth
--
jb
(replace y with x if you want to reply by e-mail)
Jakob Bieling wrote: "AGoTH" <ag***@virtools .com> wrote in message news:26******** *************** ***@posting.goo gle.com... class A { public: virtual void foo(int b) {} virtual void foo(char* b) {} };
class B : public A{ virtual void foo(int b) {} };
void foo() { B* b; b->foo("tgre"); }
this code does not compile, neither in Visual C++ nor in Codewarrior. Does someone know why ?
The 'foo(int)' in B hides 'foo(char*)' of A and overloads 'foo(int)' Define an overload for 'foo(char*)' in B, where you just forward the call to 'A::foo(int)'.
or add a using directive to class B
class B : public A
{
using A::foo;
--
Karl Heinz Buchegger kb******@gascad .at
Jakob Bieling wrote: "Karl Heinz Buchegger" <kb******@gasca d.at> wrote in message news:3F******** *******@gascad. at...
Jakob Bieling wrote:
"AGoTH" <ag***@virtools .com> wrote in message news:26***** *************** ******@posting. google.com...
class A { public: virtual void foo(int b) {} virtual void foo(char* b) {} };
class B : public A{ virtual void foo(int b) {} };
void foo() { B* b; b->foo("tgre"); }
this code does not compile, neither in Visual C++ nor in Codewarrior. Does someone know why ?
The 'foo(int)' in B hides 'foo(char*)' of A and overloads 'foo(int)' Define an overload for 'foo(char*)' in B, where you just forward the
call to
'A::foo(int) '.
or add a using directive to class B
class B : public A { using A::foo;
This one is actually much nicer! Thanks for the hint!
I was just about to ask this same question! I'm still not completely clear on
why 'foo(int)' in B hides 'foo(char*)' in A. I would have thought that the name
mangling would have created completely different symbols for 'foo(int)' and
'foo(char*)' and thus 'foo(char*)' would not be hidden.
Karl
Jakob Bieling wrote: "AGoTH" <ag***@virtools .com> wrote in message news:26******** *************** ***@posting.goo gle.com...
class A { public: virtual void foo(int b) {} virtual void foo(char* b) {} };
class B : public A{ virtual void foo(int b) {} };
void foo() { B* b; b->foo("tgre"); }
this code does not compile, neither in Visual C++ nor in Codewarrior. Does someone know why ? The 'foo(int)' in B hides 'foo(char*)' of A and overloads 'foo(int)' Define an overload for 'foo(char*)' in B, where you just forward the call to 'A::foo(int)'.
I think you meant to say:
The 'foo(int)' in B hides 'foo(char*)' of A and _overrides_ 'foo(int)'.
Define an overload for 'foo(char*)' in B, where you just forward the
call to _'A::foo(char*) '_.
Of course, a using declaration is an easier solution.
This is a pretty common question, and the basic answer is "function
overload resolution does not cross inheritance boundaries."
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
"Kevin Goodsell" <us************ *********@never box.com> wrote in message
news:3f33f6bd@s hknews01... Jakob Bieling wrote:
"AGoTH" <ag***@virtools .com> wrote in message news:26******** *************** ***@posting.goo gle.com...
class A { public: virtual void foo(int b) {} virtual void foo(char* b) {} };
class B : public A{ virtual void foo(int b) {} };
void foo() { B* b; b->foo("tgre"); }
this code does not compile, neither in Visual C++ nor in Codewarrior. Does someone know why ? The 'foo(int)' in B hides 'foo(char*)' of A and overloads 'foo(int)' Define an overload for 'foo(char*)' in B, where you just forward the
call to 'A::foo(int)'.
I think you meant to say:
The 'foo(int)' in B hides 'foo(char*)' of A and _overrides_ 'foo(int)'. Define an overload for 'foo(char*)' in B, where you just forward the call to _'A::foo(char*) '_.
Uh yes, got confused by all the overriding and hiding and overloading ;)
Thanks for the correction!
--
jb
(replace y with x if you want to reply by e-mail)
"NeilF" <ne************ *@hotmail.com> wrote... [...] Unless it's explained in the FAQ, perhaps you could take the time to
explain why hiding related member functions is useful. Perhaps it's something to
do with avoiding program misbehavior caused by implicit parameter casting?
I am fairly sure that the rationale behind name hiding has been already
given in one form or another, and if you look for it, you'll find it.
No, I don't have any links off the top of my head. And it's been some
time since I opened D&E ("Design and Evolution of C++" by Stroustrup)
last time, I just don't remember if there is some description there.
Name hiding is fundamental and is not C++ specific. In C, if you declare
a variable in a block, it would hide another variable with the same name
in the enclosing block. It's been like this for ages.
I believe that when the time came to define the relationship of derived
classes' scopes, Bjarne had only two possible outcomes: either make class
scopes follow the normal nested scopes line or make them an exception.
Perhaps making them an exception promised more unnecessary complexity and
would be more difficult to explain and justify. I don't know for sure.
Try asking in comp.std.c++. Most of Standard Committee people frequent
that newsgroup, many must remember why name hiding was extended onto class
members (or have some links handy). It's actually much better place to
ask the "why" questions than comp.lang.c++. Here we mostly talk "how".
Victor
"NeilF" <ne************ *@hotmail.com> wrote... [...] Unless it's explained in the FAQ, perhaps you could take the time to
explain why hiding related member functions is useful. Perhaps it's something to
do with avoiding program misbehavior caused by implicit parameter casting?
I am fairly sure that the rationale behind name hiding has been already
given in one form or another, and if you look for it, you'll find it.
No, I don't have any links off the top of my head. And it's been some
time since I opened D&E ("Design and Evolution of C++" by Stroustrup)
last time, I just don't remember if there is some description there.
Name hiding is fundamental and is not C++ specific. In C, if you declare
a variable in a block, it would hide another variable with the same name
in the enclosing block. It's been like this for ages.
I believe that when the time came to define the relationship of derived
classes' scopes, Bjarne had only two possible outcomes: either make class
scopes follow the normal nested scopes line or make them an exception.
Perhaps making them an exception promised more unnecessary complexity and
would be more difficult to explain and justify. I don't know for sure.
Try asking in comp.std.c++. Most of Standard Committee people frequent
that newsgroup, many must remember why name hiding was extended onto class
members (or have some links handy). It's actually much better place to
ask the "why" questions than comp.lang.c++. Here we mostly talk "how".
Victor
In article <BQeZa.106393$H o3.13941@sccrns c03>, v.********@attA bi.com
says...
[ ... ] I believe that when the time came to define the relationship of derived classes' scopes, Bjarne had only two possible outcomes: either make class scopes follow the normal nested scopes line or make them an exception. Perhaps making them an exception promised more unnecessary complexity and would be more difficult to explain and justify. I don't know for sure.
I the D&E, Bjarne also mentions that if you looked in the base class, it
could rather unexpectedly change how programs worked. Just for example,
assume a base class has f(long) and a derived class has f(long). If you
call f(1), it basically invokes derived::f(1L) (i.e. invokes the derived
version of f, converting the argument to a long in the process.
Now, if it looked in the base class as well, then adding f(int) to the
base would mean that the client code would now invoke base::f(1). A
seemingly rather innocent change in the base class might make wholesale
changes in code that doesn't use the base class directly at all.
In particular, while the current situation causes something new users
often find unexpected, it's (generally) something that becomes obvious
when you first write the client code -- typically, it won't compile at
all, because the code attempts to call a function that's no longer
visible.
The alternative is rather the opposite: it can silently change behavior
long after code is written, and many compilers would probably let you do
it without so much as a peep of warning either (though if it's a major
change in behavior, the design is probably suspect).
--
Later,
Jerry.
The universe is a figment of its own imagination.
In article <BQeZa.106393$H o3.13941@sccrns c03>, v.********@attA bi.com
says...
[ ... ] I believe that when the time came to define the relationship of derived classes' scopes, Bjarne had only two possible outcomes: either make class scopes follow the normal nested scopes line or make them an exception. Perhaps making them an exception promised more unnecessary complexity and would be more difficult to explain and justify. I don't know for sure.
I the D&E, Bjarne also mentions that if you looked in the base class, it
could rather unexpectedly change how programs worked. Just for example,
assume a base class has f(long) and a derived class has f(long). If you
call f(1), it basically invokes derived::f(1L) (i.e. invokes the derived
version of f, converting the argument to a long in the process.
Now, if it looked in the base class as well, then adding f(int) to the
base would mean that the client code would now invoke base::f(1). A
seemingly rather innocent change in the base class might make wholesale
changes in code that doesn't use the base class directly at all.
In particular, while the current situation causes something new users
often find unexpected, it's (generally) something that becomes obvious
when you first write the client code -- typically, it won't compile at
all, because the code attempts to call a function that's no longer
visible.
The alternative is rather the opposite: it can silently change behavior
long after code is written, and many compilers would probably let you do
it without so much as a peep of warning either (though if it's a major
change in behavior, the design is probably suspect).
--
Later,
Jerry.
The universe is a figment of its own imagination. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Edward Diener |
last post by:
Is it possible for a derived class to override a property and/or event of
its base class ?
|
by: Hongzheng Wang |
last post by:
Hi,
I have a problem about the overriding of private methods of base
class.
That is, if a method f() of base class is private, can the derived
class overriding f() be overriding?
For example,
class base {
|
by: franklini |
last post by:
hello people i. can anybody help me, i dont know what is wrong with
this class. it has something to do with the me trying to override the
input output stream. if i dont override it, it works fine. i would
forget overriding it but i have to do it because its a coursework. here
is a simple version of the class
#include <iostream>
#include <string>
#include <vector>
|
by: iceColdFire |
last post by:
Hi,
What is the Diff btwn Function overloading and overriding
thanks,
a.a.cpp
|
by: Susan Baker |
last post by:
Hello everybody,
I'm new to C++ (I have some C background). I've read up on this topic a
few times but it just dosen't seem to be sinking in.
1. Whats the difference between overloading and overriding?
2. When is one preferable to use as opposed to the other?
3. How are virtual functions related to this topic
(overloading/overriding) - a real world example of using virtual
functions would be very much appreciated.
| |
by: Bob Weiner |
last post by:
What is the purpose of hiding intead of overriding a method? I have googled
the question but haven't found anything that makes any sense of it.
In the code below, the only difference is that when the Poodle is upcast to
the Dog (in its wildest dreams) it then says "bow wow" where the bernard
always says "woof" (see code). Basically, it appears that I'm hiding the
poodle's speak method from everything except the poodle.
Why would I...
|
by: David Scarlett |
last post by:
Hi all,
I've got a question regarding overriding const member functions with
non-const functions.
Let's say I've got a base class defined as follows:
/*******************/
class Foo
{
public:
|
by: yashwant pinge |
last post by:
#include<iostream>
using namespace std;
class base
{
public:
void display()
{
}
};
|
by: Rick |
last post by:
One of the rules I have recommended for our new coding standard is as
follows:
"When overriding a base class virtual function, all overloads of that
base class virtual function should also be overridden. Otherwise, the
overloads of the overridden function in the base class will not be
visible from the derived class."
In other words...
|
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...
|
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...
| |
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...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |