Icario said:
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
ublic 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_,comdat
.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_,comdat
.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