473,698 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overload resolution clarification

sri
class Base
{
public:
virtual void f(int) { std::cout<<"bas e.f(int)\n";};
virtual void f(std::complex< double>) { std::cout<<"der ived.f
\n"; };
};

class Derived : public Base
{
public :
virtual void f(double) {std::cout<<"ba se.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 1446
sri wrote:
class Base
{
public:
virtual void f(int) { std::cout<<"bas e.f(int)\n";};
virtual void f(std::complex< double>) { std::cout<<"der ived.f
\n"; };
};

class Derived : public Base
{
public :
virtual void f(double) {std::cout<<"ba se.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<do uble>) 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_m ot...@yahoo.com wrote:
class Base
{
public:
virtual void f(int) { std::cout<<"bas e.f(int)\n";};
virtual void f(std::complex< double>) { std::cout<<"der ived.f
\n"; };
};
class Derived : public Base
{
public :
virtual void f(double) {std::cout<<"ba se.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<doubl e>). The conversion double to
complex<doublef ormally 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 objektorientier ter Datenverarbeitu ng
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
2090
by: Richard Hayden | last post by:
Hi, I have the following code for example: /**********************************/ #include <iostream> class C1 { public:
13
2727
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 overload resolution ? I've used parameters of type object and string here, just to illustrate the problem. Really I have a bit more deep inheritance graph, and the things get more interesting if the strongly typed overload is like override public void...
19
1932
by: Dave Raskin | last post by:
public class Base { } public class Derived : Base { } public class Service {
0
2601
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 information by googling the web. Function GetFromXml(Id As String, Node As String) Dim NodeValue As String Dim xpathDoc As XPathDocument Dim xmlNav As XPathNavigator Dim xmlNI As XPathNodeIterator
2
1847
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
1528
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
1865
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
3695
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 explicitly takes a pointer-to-member with no parameters, why is the lookup for the overloaded function not able to use the number of arguments to determine the appropriate function? Is there a relevant portion of the standard that governs the...
2
2701
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
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8895
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.