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

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 1450
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
1
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
5
by: rbfish | last post by:
Hi, How can I call a virtual base function with variable parameters? like, class base { public: int printf(const char *fmt, ...) { // do anything here.
13
by: George | last post by:
Hi all, I'm moving my experince from C to C++ and while so I'm facing some problems with inheritance. Here is my problem: class A { // All share attributes // ....
17
by: Jef Driesen | last post by:
Suppose I have a datastructure (actually it's a graph) with one template parameter (the property P for each edge and vertex): struct graph<P>; struct vertex<P>; struct edge<P>; I also have...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
1
by: joseph cook | last post by:
I'm confused by this output. I would expect the more specific overloaded function to be selected: #include <iostream> class Base { }; class Derived : public Base {};
6
by: newbie | last post by:
class AbstractBox { public: virtual double area() const = 0; } class BoxA : public AbstractBox { virtual double area() { return 0.1; } } class BoxB : public AbstractBox {
8
by: Jon Harrop | last post by:
I am trying to learn C# and .NET programming in general but I am finding it very hard going. To start with, I'd like to translate some trivial functions from other languages that I am familiar with...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.