Not virtual good function called

R

Remi Ricard

Hi,

This is a code that reproduce my problem.

#include <stdio.h>
class A {
public:
virtual int get(int a)=0;
int get(float a) {return 1;}
};

class B : public A {
public:
virtual int get(int a) { return 2; }
};

void main()
{
B b;
printf("b->get(12)=%d b->get(1.234)=%d\n", b.get(99), b.get(1.234));
}



The output is:
b->get(12)=2 b->get(1.234)=2


Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??

I think, I have a conversion from float to int then the function
B::get(int) is called. Why ??



Remi Ricard
(e-mail address removed)
 
K

Kevin Goodsell

Remi said:
Hi,

This is a code that reproduce my problem.

#include <stdio.h>
class A {
public:
virtual int get(int a)=0;
int get(float a) {return 1;}
};
class B : public A {
public:
virtual int get(int a) { return 2; }

This hides any other 'get' function that would have otherwise been
inherited. You can avoid this with

using A::get;
};

void main()

main returns int in C++. The standard requires this. void has never been
an acceptable return type for main.
{
B b;
printf("b->get(12)=%d b->get(1.234)=%d\n", b.get(99), b.get(1.234));
}



The output is:
b->get(12)=2 b->get(1.234)=2


Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??

Because A::get(float) doesn't exist for B. Overload resolution doesn't
cross inheritance boundaries.
I think, I have a conversion from float to int then the function
B::get(int) is called. Why ??

It's the only acceptable overload.

-Kevin
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Remi Ricard escribió:
#include <stdio.h>
class A {
public:
virtual int get(int a)=0;
int get(float a) {return 1;}
};

class B : public A {
public:
virtual int get(int a) { return 2; }
};

void main()
{
B b;
printf("b->get(12)=%d b->get(1.234)=%d\n", b.get(99), b.get(1.234));
}

The output is:
b->get(12)=2 b->get(1.234)=2

Why the second call b.get(1.2.3.4) don't call the function A::get(float) ??

The get function in B hides all get functions in A. If you want to make
it visible you need to put in B:

using A::get;

Regards.
 
H

Hafiz Abid Qadeer

Julián Albo said:
Remi Ricard escribi :




) ??

The get function in B hides all get functions in A. If you want to make
it visible you need to put in B:

using A::get;

Regards.

when you call b.get(1.234)), compiler first looks in B. It finds a
function that has the same name and 1.234 can be converted to its ats
argument type (int) so it stops looking further for that name. To call
the A's function, you have to use the method described in the above
postings. Hope that helps.

Regards
Hafiz Abid Qadeer
 
M

Michiel Salters

Julián Albo said:
Remi Ricard escribi :
[SNIP]
when you call b.get(1.234)), compiler first looks in B. It finds a
function that has the same name and 1.234 can be converted to its ats
argument type (int) so it stops looking further for that name.

The compiler stops even earlier. Even if you couldn't convert
1.234 to int, once the compiler has found one get() it will not
go up one inheritance level. It will search for additional get()
overloads only in the class where it found the first no matter
what type of arguments.

Regards
 

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,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top