473,386 Members | 1,741 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,386 software developers and data experts.

Inheritance not letting signature to come through.

Hi, I was posed this question and I didn't know the answer. Anybody
here know why a member function of the same name but different signature
from that of a class it inherits from is not viable unless explicitly
made so using the 'using' keyword?

Here is an example:

1 class C1
2 {
3 public:
4 void M1(int i) {}
5 };
6
7
8 class C2: public C1
9 {
10 public:
11 // using C1::M1; //< When commented, g++ and VC++ emit error
12 void M1(int i, int j) {}
13 };
14
15 int main()
16 {
17 C2 c;
18 c.M1(14); //< Error emitted here
19 c.M1(1, 2);
20 return 0;
21 }

g++ emits:
18: error: no matching function for call to `C2::M1(int)'
12: note: candidates are: void C2::M1(int, int)

Thanks,
Adrian

--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
May 25 '07 #1
7 1591
Adrian Hawryluk wrote:
Hi, I was posed this question and I didn't know the answer. Anybody
here know why a member function of the same name but different signature
from that of a class it inherits from is not viable unless explicitly
made so using the 'using' keyword?

Here is an example:

1 class C1
2 {
3 public:
4 void M1(int i) {}
5 };
6
7
8 class C2: public C1
9 {
10 public:
11 // using C1::M1; //< When commented, g++ and VC++ emit error
12 void M1(int i, int j) {}
13 };
14
15 int main()
16 {
17 C2 c;
18 c.M1(14); //< Error emitted here
19 c.M1(1, 2);
20 return 0;
21 }

g++ emits:
18: error: no matching function for call to `C2::M1(int)'
12: note: candidates are: void C2::M1(int, int)

Because the standard says so in 10.2.2:
"A member name f in one sub-object B hides a member name f in a
sub-object A if A is a base class sub-object of B. Any declarations
that are so hidden are eliminated from consideration."

Someone around here probably knows the actual justification for that
being in the standard.

--
Alan Johnson
May 25 '07 #2
On May 25, 7:29 pm, Alan Johnson <a...@yahoo.comwrote:
Adrian Hawryluk wrote:
Hi, I was posed this question and I didn't know the answer. Anybody
here know why a member function of the same name but different signature
from that of a class it inherits from is not viable unless explicitly
made so using the 'using' keyword?
Here is an example:
1 class C1
2 {
3 public:
4 void M1(int i) {}
5 };
8 class C2: public C1
9 {
10 public:
11 // using C1::M1; //< When commented, g++ and VC++ emit error
12 void M1(int i, int j) {}
13 };
15 int main()
16 {
17 C2 c;
18 c.M1(14); //< Error emitted here
19 c.M1(1, 2);
20 return 0;
21 }
g++ emits:
18: error: no matching function for call to `C2::M1(int)'
12: note: candidates are: void C2::M1(int, int)
Because the standard says so in 10.2.2:
"A member name f in one sub-object B hides a member name f in a
sub-object A if A is a base class sub-object of B. Any declarations
that are so hidden are eliminated from consideration."
Someone around here probably knows the actual justification for that
being in the standard.
One possible reason is so that adding functions to the base
class won't break the derived class. Consider something like:

class Base
{
} ;

class Derived : public Base
{
public:
void f( int i ) { /* ... */ }
void g( char c ) { /* ... */ ; f( c ) ; /* ... */ }
} ;

What happens now if you add a function "void f( int )" to base?
May 25 '07 #3
James Kanze wrote:
On May 25, 7:29 pm, Alan Johnson <a...@yahoo.comwrote:
>Because the standard says so in 10.2.2:
"A member name f in one sub-object B hides a member name f in a
sub-object A if A is a base class sub-object of B. Any declarations
that are so hidden are eliminated from consideration."
>Someone around here probably knows the actual justification for that
being in the standard.

One possible reason is so that adding functions to the base
class won't break the derived class. Consider something like:

class Base
{
} ;

class Derived : public Base
{
public:
void f( int i ) { /* ... */ }
void g( char c ) { /* ... */ ; f( c ) ; /* ... */ }
} ;

What happens now if you add a function "void f( int )" to base?

In this case, without 10.2.2, you would have an ambiguity but I think
adding a function "void f( char )" to the base makes the potential issue
even more obviously horrible, as you now have a better match in the base
class, changing the behaviour of Derived with (theoretically) no
compiler warning.
May 26 '07 #4
James Kanze wrote:
On May 25, 7:29 pm, Alan Johnson <a...@yahoo.comwrote:
>Adrian Hawryluk wrote:
>>Hi, I was posed this question and I didn't know the answer. Anybody
here know why a member function of the same name but different signature
from that of a class it inherits from is not viable unless explicitly
made so using the 'using' keyword?
>>Here is an example:
>> 1 class C1
2 {
3 public:
4 void M1(int i) {}
5 };
>> 8 class C2: public C1
9 {
10 public:
11 // using C1::M1; //< When commented, g++ and VC++ emit error
12 void M1(int i, int j) {}
13 };
>> 15 int main()
16 {
17 C2 c;
18 c.M1(14); //< Error emitted here
19 c.M1(1, 2);
20 return 0;
21 }
>>g++ emits:
18: error: no matching function for call to `C2::M1(int)'
12: note: candidates are: void C2::M1(int, int)
>Because the standard says so in 10.2.2:
"A member name f in one sub-object B hides a member name f in a
sub-object A if A is a base class sub-object of B. Any declarations
that are so hidden are eliminated from consideration."
>Someone around here probably knows the actual justification for that
being in the standard.

One possible reason is so that adding functions to the base
class won't break the derived class. Consider something like:

class Base
{
} ;

class Derived : public Base
{
public:
void f( int i ) { /* ... */ }
void g( char c ) { /* ... */ ; f( c ) ; /* ... */ }
} ;

What happens now if you add a function "void f( int )" to base?

Since in the call to g, this is a Derived*, would this not just call
Derived::f(int)?

That is not scary at all.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
May 26 '07 #5
Charles Bailey wrote:
James Kanze wrote:
>On May 25, 7:29 pm, Alan Johnson <a...@yahoo.comwrote:
>>Because the standard says so in 10.2.2:
"A member name f in one sub-object B hides a member name f in a
sub-object A if A is a base class sub-object of B. Any declarations
that are so hidden are eliminated from consideration."
>>Someone around here probably knows the actual justification for that
being in the standard.

One possible reason is so that adding functions to the base
class won't break the derived class. Consider something like:

class Base
{
} ;

class Derived : public Base
{
public:
void f( int i ) { /* ... */ }
void g( char c ) { /* ... */ ; f( c ) ; /* ... */ }
} ;

What happens now if you add a function "void f( int )" to base?


In this case, without 10.2.2, you would have an ambiguity but I think
adding a function "void f( char )" to the base makes the potential issue
even more obviously horrible, as you now have a better match in the base
class, changing the behaviour of Derived with (theoretically) no
compiler warning.
That I can see as a problem. Thanks.
Adrian
--
__________________________________________________ ___________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ _---_ Q. What are you doing here? _---_ /
\ / | A. Just surf'n the net, teaching and | \ /
\__/___\___ learning, learning and teaching. You?_____/___\__/
\/______[blog:__http://adrians-musings.blogspot.com/]______\/
May 26 '07 #6
On May 26, 1:56 am, Charles Bailey <usenetspa...@hashpling.orgwrote:
James Kanze wrote:
On May 25, 7:29 pm, Alan Johnson <a...@yahoo.comwrote:
Because the standard says so in 10.2.2:
"A member name f in one sub-object B hides a member name f in a
sub-object A if A is a base class sub-object of B. Any declarations
that are so hidden are eliminated from consideration."
Someone around here probably knows the actual justification for that
being in the standard.
One possible reason is so that adding functions to the base
class won't break the derived class. Consider something like:
class Base
{
} ;
class Derived : public Base
{
public:
void f( int i ) { /* ... */ }
void g( char c ) { /* ... */ ; f( c ) ; /* ... */ }
} ;
What happens now if you add a function "void f( int )" to base?
In this case, without 10.2.2, you would have an ambiguity but I think
adding a function "void f( char )" to the base makes the potential issue
even more obviously horrible, as you now have a better match in the base
class, changing the behaviour of Derived with (theoretically) no
compiler warning.
Yes. That's actually the example that I meant to post. Adding
the function to Base silently causes the semantics of
Derived:g() to change.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
May 26 '07 #7
On May 26, 1:16 pm, Adrian Hawryluk <adrian.hawryluk-at-
gmail....@nospam.comwrote:
James Kanze wrote:
On May 25, 7:29 pm, Alan Johnson <a...@yahoo.comwrote:
Adrian Hawryluk wrote:
[...]
Because the standard says so in 10.2.2:
"A member name f in one sub-object B hides a member name f in a
sub-object A if A is a base class sub-object of B. Any declarations
that are so hidden are eliminated from consideration."
Someone around here probably knows the actual justification for that
being in the standard.
One possible reason is so that adding functions to the base
class won't break the derived class. Consider something like:
class Base
{
} ;
class Derived : public Base
{
public:
void f( int i ) { /* ... */ }
void g( char c ) { /* ... */ ; f( c ) ; /* ... */ }
} ;
What happens now if you add a function "void f( int )" to base?
Since in the call to g, this is a Derived*, would this not just call
Derived::f(int)?
Sorry. The added function should have been void Base::f(char).
If both Base::f(char) and Derived::f(int) were available,
overload resolution will pick up the first when calling f() with
a char. This results in a silent change in the semantics of
Derived::g().

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 26 '07 #8

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

Similar topics

4
by: Roy Pereira | last post by:
I have an application that is composed of a set of "Content" dlls and a viewer application. The viewer calls a standard set of functions that are present in all the dlls. I maintain this by...
2
by: Tim Mackey | last post by:
hi folks, i'm puzzled over this one, anyone with some solid db experience might be able to enlighten me here. i'm modelling a file system in a database as follows, and i can't figure out to...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
7
by: Hazz | last post by:
Are there any good references/articles/books which provide clarity toward my insecurity still on deciding how to model a complex system? I still feel uncomfortable with my understanding, even...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.