Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   overload resolution clarification (http://www.velocityreviews.com/forums/t518350-overload-resolution-clarification.html)

sri 06-28-2007 01:52 PM

overload resolution clarification
 
class Base
{
public:
virtual void f(int) { std::cout<<"base.f(int)\n";};
virtual void f(std::complex<double>) { std::cout<<"derived.f
\n"; };
};

class Derived : public Base
{
public :
virtual void f(double) {std::cout<<"base.f(double)\n";};
};

int main()
{

Base b; //Line 1
Derived d; //Line 2
Base* pb = new Derived; //Line 3
pb->f(1.0f); //Line 4
return 0;
}

the above program is calling f(int) version, why the overloading
resolution is calling f(int) version in Base class?

with Regards,
Sri


Victor Bazarov 06-28-2007 01:59 PM

Re: overload resolution clarification
 
sri wrote:
> class Base
> {
> public:
> virtual void f(int) { std::cout<<"base.f(int)\n";};
> virtual void f(std::complex<double>) { std::cout<<"derived.f
> \n"; };
> };
>
> class Derived : public Base
> {
> public :
> virtual void f(double) {std::cout<<"base.f(double)\n";};
> };
>
> int main()
> {
>
> Base b; //Line 1
> Derived d; //Line 2
> Base* pb = new Derived; //Line 3
> pb->f(1.0f); //Line 4
> return 0;
> }
>
> the above program is calling f(int) version, why the overloading
> resolution is calling f(int) version in Base class?


Instead of which one, the 'complex'? User-defined conversion sequence
(float to std::complex<double>) has lower rank than standard (floating-
integral) conversion, according to 13.3.3.2/2.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



James Kanze 06-29-2007 07:36 AM

Re: overload resolution clarification
 
On Jun 28, 3:52 pm, sri <srinivasarao_mot...@yahoo.com> wrote:
> class Base
> {
> public:
> virtual void f(int) { std::cout<<"base.f(int)\n";};
> virtual void f(std::complex<double>) { std::cout<<"derived.f
> \n"; };
> };


> class Derived : public Base
> {
> public :
> virtual void f(double) {std::cout<<"base.f(double)\n";};
> };


> int main()
> {
> Base b; //Line 1
> Derived d; //Line 2
> Base* pb = new Derived; //Line 3
> pb->f(1.0f); //Line 4
> return 0;
> }


> the above program is calling f(int) version, why the overloading
> resolution is calling f(int) version in Base class?


And what should it call? Overload resolution is done by the
compiler, at compile time, and so can only consider the static
type of the epxression. In this case, Base. There are two f in
Base, f(int) and f(complex<double>). The conversion double to
complex<double> formally involves a user defined conversion;
according to the rules, the compiler chooses a built-in
conversion over a user defined conversion, even if the built-in
conversion is lossy, as it is here.

And of course, since the static type is Base, the compiler
doesn't see the f(double) in derived at all.

--
James Kanze (GABI Software) email:james.kanze@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



All times are GMT. The time now is 08:12 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.