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/]______\/ 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
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?
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.
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/]______\/
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/]______\/
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Mark |
last post by:
why doesn't .NET support multiple inheritance?
I think it's so silly!
Cheers,
Mark
|
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...
|
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"...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |