470,870 Members | 1,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

function with derived class arguments

I have an error when i compile this:

class A {
public:
void f(A&) {}
};

class B: public A {
public:
void f(B&) {}
};

int main() {
A a;
B b;
b.f(a);
}

my compiler says:

no matching function for call to 'B::f(A&)'

I dont understand why the base class member function is not seen by the
compiler ?

Txn
Jun 27 '08 #1
7 1347
Icario wrote:
I have an error when i compile this:

class A {
public:
void f(A&) {}
};

class B: public A {
public:
void f(B&) {}
};

int main() {
A a;
B b;
b.f(a);
}

my compiler says:

no matching function for call to 'B::f(A&)'

I dont understand why the base class member function is not seen by the
compiler ?
Because the language definition says so.

You need to make A::f visible.

class A {
public:
void f(A&) {}
};

class B: public A {
public:
using A::f; // <------------ !!!

void f(B&) {}
};

int main() {
A a;
B b;
b.f(a);
}
Best

Kai-Uwe Bux

Jun 27 '08 #2
Icario wrote:
>
>I have an error when i compile this:

class A {
public:
void f(A&) {}
};

class B: public A {
public:
void f(B&) {}
};

int main() {
A a;
B b;
b.f(a);
}

my compiler says:

no matching function for call to 'B::f(A&)'

I dont understand why the base class member function is not seen by the
compiler ?

Because the language definition says so.
Ok but what is the reason for that ?
Jun 27 '08 #3
Icario <ic****@granamail.cmowrites:
>Icario wrote:
>>I have an error when i compile this:

class A {
public:
void f(A&) {}
};

class B: public A {
public:
void f(B&) {}
};

int main() {
A a;
B b;
b.f(a);
}

my compiler says:

no matching function for call to 'B::f(A&)'

I dont understand why the base class member function is not seen by the
compiler ?

Because the language definition says so.

Ok but what is the reason for that ?
Because A::f(A&) and B::f(B&) are totally unrelated methods. They're
so unrelated, that the compiler generates different names for them:
(see below the ==== line).
The true name of A::f(A&) is _ZN1A1fERS_ (on some compiler)
while the true name of B::f(B&) is _ZN1B1fERS_ (on same compiler)
(Or did you believe name mangling was done only to obfuscate?).

Explicitely, methods are not identified by only their names, but also
by their signature. To do what you wanted to do, you would have to
keep both the name and the signature constant:

-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Thu Jun 5 12:34:11

cd /tmp ; g++ -o b b.c++ && (cat b.c++ ; echo '-----------' ; ./b)
#include <iostream>
using namespace std;
class A{
public:
int f(A&){return 42;}
};
class B:public A{
public:
int f(A&){return 33;}
};
int main(void){
A a;
B b;
cout<<a.f(a)<<endl;
cout<<b.f(b)<<endl; // works too, since b is a B is a kind of A.
return(0);
}
-----------
42
33

Compilation finished at Thu Jun 5 12:34:12

In this case, we only have one signature int f(A&), and two methods,
one of which is selected according to the (static) class of the
recipient of the message. (You'd use virtual if you wanted the
dispatch to be done at run-time, on the effective (dynamic) class of
the recipient.)
================================================== ======================

-*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Thu Jun 5 12:29:10

cd /tmp ; g++ -S a.s a.c++ ; cat a.c++ a.s
class A {
public:
int f(A&) {return 42;}
};

class B: public A {
public:
using A::f;
int f(B&) { return 33;}
};

int main() {
A a;
B b;
b.f(a);
b.f(b);
}
.file "a.c++"
.section .text._ZN1A1fERS_,"axG",@progbits,_ZN1A1fERS_,comd at
.align 2
.weak _ZN1A1fERS_
.type _ZN1A1fERS_, @function
_ZN1A1fERS_:
..LFB2:
pushq %rbp
..LCFI0:
movq %rsp, %rbp
..LCFI1:
movq %rdi, -8(%rbp)
movq %rsi, -16(%rbp)
movl $42, %eax
leave
ret
..LFE2:
.size _ZN1A1fERS_, .-_ZN1A1fERS_
..globl __gxx_personality_v0
.section .text._ZN1B1fERS_,"axG",@progbits,_ZN1B1fERS_,comd at
.align 2
.weak _ZN1B1fERS_
.type _ZN1B1fERS_, @function
_ZN1B1fERS_:
..LFB3:
pushq %rbp
..LCFI2:
movq %rsp, %rbp
..LCFI3:
movq %rdi, -8(%rbp)
movq %rsi, -16(%rbp)
movl $33, %eax
leave
ret
..LFE3:
.size _ZN1B1fERS_, .-_ZN1B1fERS_
.text
.align 2
..globl main
.type main, @function
main:
..LFB4:
pushq %rbp
..LCFI4:
movq %rsp, %rbp
..LCFI5:
subq $16, %rsp
..LCFI6:
leaq -2(%rbp), %rdi
leaq -1(%rbp), %rsi
call _ZN1A1fERS_
leaq -2(%rbp), %rsi
leaq -2(%rbp), %rdi
call _ZN1B1fERS_
movl $0, %eax
leave
ret
..LFE4:
.size main, .-main
.section .eh_frame,"a",@progbits
..Lframe1:
.long .LECIE1-.LSCIE1
..LSCIE1:
.long 0x0
.byte 0x1
.string "zPR"
.uleb128 0x1
.sleb128 -8
.byte 0x10
.uleb128 0x6
.byte 0x3
.long __gxx_personality_v0
.byte 0x3
.byte 0xc
.uleb128 0x7
.uleb128 0x8
.byte 0x90
.uleb128 0x1
.align 8
..LECIE1:
..LSFDE1:
.long .LEFDE1-.LASFDE1
..LASFDE1:
.long .LASFDE1-.Lframe1
.long .LFB2
.long .LFE2-.LFB2
.uleb128 0x0
.byte 0x4
.long .LCFI0-.LFB2
.byte 0xe
.uleb128 0x10
.byte 0x86
.uleb128 0x2
.byte 0x4
.long .LCFI1-.LCFI0
.byte 0xd
.uleb128 0x6
.align 8
..LEFDE1:
..LSFDE3:
.long .LEFDE3-.LASFDE3
..LASFDE3:
.long .LASFDE3-.Lframe1
.long .LFB3
.long .LFE3-.LFB3
.uleb128 0x0
.byte 0x4
.long .LCFI2-.LFB3
.byte 0xe
.uleb128 0x10
.byte 0x86
.uleb128 0x2
.byte 0x4
.long .LCFI3-.LCFI2
.byte 0xd
.uleb128 0x6
.align 8
..LEFDE3:
..LSFDE5:
.long .LEFDE5-.LASFDE5
..LASFDE5:
.long .LASFDE5-.Lframe1
.long .LFB4
.long .LFE4-.LFB4
.uleb128 0x0
.byte 0x4
.long .LCFI4-.LFB4
.byte 0xe
.uleb128 0x10
.byte 0x86
.uleb128 0x2
.byte 0x4
.long .LCFI5-.LCFI4
.byte 0xd
.uleb128 0x6
.align 8
..LEFDE5:
.ident "GCC: (GNU) 4.1.2 (Gentoo 4.1.2 p1.0.2)"
.section .note.GNU-stack,"",@progbits

Compilation finished at Thu Jun 5 12:29:10

--
__Pascal Bourguignon__
Jun 27 '08 #4
Pascal J. Bourguignon dixit:
Icario <ic****@granamail.cmowrites:
>>Icario wrote:

I have an error when i compile this:

class A {
public:
void f(A&) {}
};

class B: public A {
public:
void f(B&) {}
};

int main() {
A a;
B b;
b.f(a);
}

my compiler says:

no matching function for call to 'B::f(A&)'

I dont understand why the base class member function is not seen by the
compiler ?
Because the language definition says so.
Ok but what is the reason for that ?

Because A::f(A&) and B::f(B&) are totally unrelated methods.
Yes. That's right. I know that.
They're
so unrelated, that the compiler generates different names for them:
(see below the ==== line).
The true name of A::f(A&) is _ZN1A1fERS_ (on some compiler)
while the true name of B::f(B&) is _ZN1B1fERS_ (on same compiler)
(Or did you believe name mangling was done only to obfuscate?).
No I did not believe that.
>
....skip unbearable assembly....
>
I was just trying to understand why the standard makes A::f(A&) hidden
(unless using artefacts as suggested) in B inherited from class A when
defining a different function (different signature) with same name.
Jun 27 '08 #5
On 5 Jun, 18:37, Icario <ica...@granamail.cmowrote:
[snip]
I was just trying to understand why the standard makes A::f(A&) hidden
(unless using artefacts as suggested) in B inherited from class A when
defining a different function (different signature) with same name.
Because of the way name lookup works in C++, and I believe it works
this way to avoid (even more) ambiguities. For instance:

#include <string>
struct B
{
void f(int) {}
void g(unsigned) {}
void h(float) {}
};
struct D : B
{
void f(std::string) {}
void g(int) {}
void h(double) {}
};

int main()
{
D d;
d.f(0); // if B's names were considered, which function should be
called here?
d.g(0u); // ,here?
d.h(0.0f); // and here?
}

Hope this helps,
DP
Jun 27 '08 #6
On Jun 5, 12:35 pm, Icario <ica...@granamail.cmowrote:
I have an error when i compile this:

class A {
public:
void f(A&) {}

};

class B: public A {
public:
void f(B&) {}

};

int main() {
A a;
B b;
b.f(a);

}

my compiler says:

no matching function for call to 'B::f(A&)'

I dont understand why the base class member function is not seen by the
compiler ?

Txn
As you are declaring a function in the derived class B with the same
name of the function present in the base class A, so derived class's
function hides the base class's function and the A::f(A&) is no more
visible for the derived class.
Jun 27 '08 #7
On Jun 5, 9:59 am, Icario <ica...@granamail.cmowrote:
Icario wrote:
I have an error when i compile this:
class A {
public:
void f(A&) {}
};
class B: public A {
public:
void f(B&) {}
};
int main() {
A a;
B b;
b.f(a);
}
my compiler says:
no matching function for call to 'B::f(A&)'
I dont understand why the base class member function is not
seen by the compiler ?
Because the language definition says so.
Ok but what is the reason for that ?
To avoid problems for the maintenance programmer. Consider
something like:

class Base
{
} ;

class Derived : public Base
{
void f( int ) ;
void g() { f( 'a' ) ; }
} ;

Obviously, as it stands, the programmer wants (and expects) the
call to f in g to go to Derived::f(). What happens if during
maintenance, a programmer adds f(char) to Base? Without name
hiding, if the new function is private, the code suddenly stops
compiling, and if it isn't, the semantics of the program
silently change (which is even worse).

--
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
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Kostatus | last post: by
1 post views Thread by TheOne | last post: by
13 posts views Thread by George | last post: by
17 posts views Thread by Jef Driesen | last post: by
1 post views Thread by joseph cook | last post: by
6 posts views Thread by newbie | last post: by
8 posts views Thread by Jon Harrop | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.