function with derived class arguments

I

Icario

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
 
K

Kai-Uwe Bux

Icario said:
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
 
P

Pascal J. Bourguignon

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: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_,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
 
I

Icario

Pascal J. Bourguignon dixit:
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.
 
T

Triple-DES

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
 
S

sumsin

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.
 
J

James Kanze

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,172
Messages
2,570,934
Members
47,478
Latest member
ReginaldVi

Latest Threads

Top