473,735 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Method overloading and inheritance

Hi everyone,

Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};

void A::method(int n){
fprintf(stderr, "A::method(%d)\ n", n);
}

void B::method(){
fprintf(stderr, "B::method\ n");
}

int main(int argc, char** argv){

B b;
b.method(3);
return 0;
}
-----------------

mike@nemesis:~/work/inh_test$ g++ -o main main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:31: error: no matching function for call to 'B::method(int) '
main.cpp:18: note: candidates are: virtual void B::method()
Oct 31 '06 #1
10 3378
Mihai Osian wrote:
Hi everyone,

Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};
Read about "name hiding" in the FAQ or on Google.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 31 '06 #2
Mihai Osian wrote:
Hi everyone,

Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};

void A::method(int n){
fprintf(stderr, "A::method(%d)\ n", n);
}

void B::method(){
fprintf(stderr, "B::method\ n");
}

int main(int argc, char** argv){

B b;
b.method(3);
return 0;
}
-----------------

mike@nemesis:~/work/inh_test$ g++ -o main main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:31: error: no matching function for call to 'B::method(int) '
main.cpp:18: note: candidates are: virtual void B::method()
You haven't over-ridden the virtual function; you have hidden it since
your virtual function in B has a different function signature than that
in A.

Cheers! --M

Oct 31 '06 #3
Mihai Osian wrote:
Hi everyone,

Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
using A::method;
};
The prototype in B for method() hides all the function in A with the name
"method". You can use the keyword using as above to unhide the functions in A.

--

Adrian
Oct 31 '06 #4
* Mihai Osian:
[rearranged]
>
Given the code below,
-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};

void A::method(int n){
fprintf(stderr, "A::method(%d)\ n", n);
}

void B::method(){
fprintf(stderr, "B::method\ n");
}

int main(int argc, char** argv){

B b;
b.method(3);
return 0;
}
-----------------

can anyone tell me:
a) Is this normal behaviour ?
A::method(int) to be inherited by B.
mike@nemesis:~/work/inh_test$ g++ -o main main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:31: error: no matching function for call to 'B::method(int) '
main.cpp:18: note: candidates are: virtual void B::method()
Yes, it's normal behavior.

b) If it is, what is the reason behind it ?
Class B's 'method' declaration hides the inherited member function so
that it's not directly accessible.

The inherited member function can still be accessed via an A& or A*
referring to a B object. Or, you can make it available by including

using A::method;

in class B. Or by providing a wrapper in class B.

One rationale that's been put forward is that pure additions to class A,
such as introducing an overload of 'method', should not affect client
code using class B, unless that's explicitly specified in class B.
However, that ignores the fact that a B object is-an A wrt.
polymorphism, and can be referenced via A& or A* (oops, that client
code's still affected by pure additions to A). And it also ignores the
fact that this behavior is highly counter-intuitive, and the fact that
it restricts is-a behavior to runtime polymorphism, not supporting
compile time polymorphism, that is, template code working well with A
may not compile with B (counter-argument is that template code designed
for A may not otherwise necessarily have the intended effect with B (and
counter-counter that that problem is there anyway)).

I would expect the A::method(int) to be inherited by B.
It is; it's just not directly accessible the way the code is currently.

Btw., why don't you just use std::cerr instead of fprintf?
--
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?
Oct 31 '06 #5
Mihai Osian wrote:
Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};
Overrides must match a signature, which is (roughly) everything from
'method' to the ';'. That includes the int, so you really have two
signatures there. You can test this by independently overloading
method(int).

Next; don't do it. Never willingly give any two things the same name unless
you expressly expect them to override each other. A real-life method(int)
should instead have a deliberately different name.

Your experiment reveals "hiding", where the name of method() in one scope
hides a method with the same name but a different signature in another
scope. That's exactly the same kind of hiding as this:

int x = 42;
{
float x = 42.0;
assert(sizeof (float) = sizeof x);
}
assert(sizeof (int) = sizeof x);

The x in the inner scope hides the one in the outer scope.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Oct 31 '06 #6

Mihai Osian wrote:
Hi everyone,

Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(int) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};

void A::method(int n){
fprintf(stderr, "A::method(%d)\ n", n);
}

void B::method(){
fprintf(stderr, "B::method\ n");
}

int main(int argc, char** argv){

B b;
b.method(3);
return 0;
}
-----------------

mike@nemesis:~/work/inh_test$ g++ -o main main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:31: error: no matching function for call to 'B::method(int) '
main.cpp:18: note: candidates are: virtual void B::method()
The member function B::method() hides all/any member functions that A
has with the name method(...), Regardless of the parameters involved
and it doesn't matter whether A::method(..) is virtual or not You could
however, call any version of A::method(..) from within B::method().

void B::method()
{
fprintf(stderr, "B::method\ n");
A::method(99);
}

In a case like this, i'ld probably make A::method(int) pure virtual.

Oct 31 '06 #7
Alf P. Steinbach wrote:
* Mihai Osian:
[rearranged]
>>
Given the code below,
-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};

void A::method(int n){
fprintf(stderr, "A::method(%d)\ n", n);
}

void B::method(){
fprintf(stderr, "B::method\ n");
}

int main(int argc, char** argv){

B b;
b.method(3);
return 0;
}
-----------------

can anyone tell me:
a) Is this normal behaviour ?
A::method(in t) to be inherited by B.
mike@nemesis :~/work/inh_test$ g++ -o main main.cpp
main.cpp: In function 'int main(int, char**)':
main.cpp:31: error: no matching function for call to 'B::method(int) '
main.cpp:18: note: candidates are: virtual void B::method()

Yes, it's normal behavior.
Thanks, apparently everybody knew that, but me :) I've learned something
new today.
>
>b) If it is, what is the reason behind it ?

Class B's 'method' declaration hides the inherited member function so
that it's not directly accessible.

The inherited member function can still be accessed via an A& or A*
referring to a B object. Or, you can make it available by including

using A::method;

in class B. Or by providing a wrapper in class B.

One rationale that's been put forward is that pure additions to class A,
such as introducing an overload of 'method', should not affect client
code using class B, unless that's explicitly specified in class B.
I am not familiar with the term "pure addition", so before I start
arguing I will have to do my Google homework first.

However, that ignores the fact that a B object is-an A wrt.
polymorphism, and can be referenced via A& or A* (oops, that client
code's still affected by pure additions to A). And it also ignores the
fact that this behavior is highly counter-intuitive, and the fact that
it restricts is-a behavior to runtime polymorphism, not supporting
compile time polymorphism, that is, template code working well with A
may not compile with B (counter-argument is that template code designed
for A may not otherwise necessarily have the intended effect with B (and
counter-counter that that problem is there anyway)).
Sorry, you lost me. A concrete example would be more useful. To me,
the current behaviour is counter-intuitive. IMHO any change in the
ancestor class is _supposed_ to change the behaviour of all descendents.
>
>I would expect the A::method(int) to be inherited by B.

It is; it's just not directly accessible the way the code is currently.

Btw., why don't you just use std::cerr instead of fprintf?
Old habbits, I guess. The first thing I learned (long ago) in C/C++
was "printf".
>
Anyway, thanks for the answer. Apparently I think differently than
everybody else. Which probably means I'm wrong :)

Mihai

Oct 31 '06 #8
Phlip wrote:
Mihai Osian wrote:
>Given the code below, can anyone tell me:
a) Is this normal behaviour ?
b) If it is, what is the reason behind it ? I would expect the
A::method(in t) to be inherited by B.

Compiler: gcc 4.1, Linux

Thanks,
Mihai

-----------------------

#include <stdio.h>

class A{
public:
virtual void method(int);
};

class B: public A{
public:
virtual void method();
};

Overrides must match a signature, which is (roughly) everything from
'method' to the ';'. That includes the int, so you really have two
signatures there. You can test this by independently overloading
method(int).

Next; don't do it. Never willingly give any two things the same name unless
you expressly expect them to override each other. A real-life method(int)
should instead have a deliberately different name.

Actually, I have to write C++ wrapper classes around JNI calls (Java
Native Intefaces), which will mirror the functionality of the Java peer
classes.
The Java classes are organized in the way I described, so it is not
really my decision.

>
Your experiment reveals "hiding", where the name of method() in one scope
hides a method with the same name but a different signature in another
scope. That's exactly the same kind of hiding as this:

int x = 42;
{
float x = 42.0;
assert(sizeof (float) = sizeof x);
}
assert(sizeof (int) = sizeof x);

The x in the inner scope hides the one in the outer scope.
I don't think your argument is valid. In your outer scope there can
be only one "x" variable. In my "outer scope" - class A, there can be
any number of "method"s, with different signatures.
What you probably mean is that overloaded methods are just "flavours"
of a single function, so I should either overload them all, or overload
none. Except that I don't agree with this point of view. By the way -
the people who designed Java seem to share my opinion. Please, please, I
don't want to start a Java versus C++ flamewar. I am a C++ developer, it
just happens that I agree with the Java approach on this one.

Mihai






Oct 31 '06 #9
Mihai Osian wrote:
What you probably mean is that overloaded methods are just "flavours"
of a single function, so I should either overload them all, or overload
none. Except that I don't agree with this point of view. By the way -
the people who designed Java seem to share my opinion. Please, please, I
don't want to start a Java versus C++ flamewar. I am a C++ developer, it
just happens that I agree with the Java approach on this one.
If you are writing C++ code you must agree with the C++ standard. Trying to
change it will take several years, in the best case.

--
Salu2
Oct 31 '06 #10

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

Similar topics

17
4722
by: Terje Slettebø | last post by:
To round off my trilogy of "why"'s about PHP... :) If this subject have been discussed before, I'd appreciate a pointer to it. I again haven't found it in a search of the PHP groups. The PHP manual mentions "overloading" (http://no.php.net/manual/en/language.oop5.overloading.php), but it isn't really overloading at all... Not in the sense it's used in other languages supporting overloading (such as C++ and Java). As one of the...
1
2036
by: Fuzzyman | last post by:
I've been programming in python for a few months now - and returning to programming after a gap of about ten years I've really enjoyed learning python. I've just made my first forays into inheritance and operator overloading (both concepts that I initially found hard to grasp). I've written a simple config file parser - and I thought I'd experiment with making the interface easier by subclassing dict and overloading the __setitem__,...
4
6479
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. Note that the overload is identical to the specialization except, of course, for the missing "template <>". I don't know if my questions will be a bit too broad or not, but I thought I'd give it shot... When is overloading preferable to...
19
10165
by: jacob navia | last post by:
C++ introduced an interesting feature (among others): operator overloading. The idea is to build a mechanism for the user defining its own number types and the operations to be done with them. I decided to build it in the lcc-win32 distribution, and this feature allowed me to introduce 350 bit floats. Yes, 350. We have a lot of fast machines now. Why not use them? The extra-precision is useful in some algorithms.
6
3283
by: apm | last post by:
Recently I have had to use a value type for a complex structure because I don't know how to override the = operator. Can the operator ever be overloaded? Or can inheritance be used with value types?
6
2851
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually obtain with two classes derived from an abstract base class containing only the interface functions. So, when it's worth to use overloading mechanism instead of abstract/inherited?
11
28140
by: placid | last post by:
Hi all, Is it possible to be able to do the following in Python? class Test: def __init__(self): pass def puts(self, str): print str
0
1069
by: David Boddie | last post by:
On Mon May 26 17:37:04 CEST 2008, Alex Gusarov wrote: Right. I vaguely remember someone showing something like this at EuroPython a couple of years ago. I believe that this approach is actually registering an event handler (or callback) to handle a certain type of event. Just out of interest, given that you have to put the event handler somewhere, why is it a problem to derive a new class and reimplement paintCell()?
3
3893
by: puzzlecracker | last post by:
Are static method inheritable under csharp rules, if so how can they be used? Can they be virtual? They reason I am asking is because operators are static, and I don't understand how can we apply operators to base and derive classes simultaneously?
0
8964
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
8786
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9466
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
9327
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
9201
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...
1
6747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
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();...
1
3277
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
3
2190
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.