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

overload resolution clarification

sri
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

Jun 28 '07 #1
2 1428
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
Jun 28 '07 #2
On Jun 28, 3:52 pm, sri <srinivasarao_mot...@yahoo.comwrote:
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<doubleformally 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: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 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
13
by: Vladimir Granitsky | last post by:
Hi guys, Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!? Am I right that C# compiler does wrong...
19
by: Dave Raskin | last post by:
public class Base { } public class Derived : Base { } public class Service {
0
by: Eph0nk | last post by:
Hi, I get an overload resolution failed error (Overload resolution failed because no accessible 'New' can be called without a narrowing conversion), and I can't seem to find a lot of relevant...
2
by: Michi Henning | last post by:
Hi, the following code produces an error on the second-last line: Interface Left Sub op() End Interface Interface Right Sub op(ByVal i As Integer)
3
by: Christof Nordiek | last post by:
Given the following code Is there any way to call one of the Bar methods. using System; class Program { static void Main() { Foo<string, stringfoo = new Foo<string,string>();
2
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
2
by: zbigniew | last post by:
Can someone explain me how overload resolution works in C++? For example if I have function void f(char, int); and I will call f('A', 3.1) or f(1.5, 3.1F) what would be the result? Thanks
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.