473,399 Members | 2,159 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,399 software developers and data experts.

Overloaded function

Hi ,
i have a sample code like this for Overloading property in C++

i have defined these functions

Expand|Select|Wrap|Line Numbers
  1.     void fun(short a1)
  2.       {
  3.         //to do code here
  4.       }  
  5.       void fun(int a1)
  6.       {
  7.         //to do code here
  8.       }
and now in main i make a call like this

Expand|Select|Wrap|Line Numbers
  1. fun(10)
which function will get invoke in this case

Similarly if i have functions like this

Expand|Select|Wrap|Line Numbers
  1.     void fun(long a1)
  2.       {
  3.         //to do code here
  4.       }  
  5.       void fun(int a1)
  6.       {
  7.         //to do code here
  8.       }
and in main ->
Expand|Select|Wrap|Line Numbers
  1. fun(10) 
which function will get invoke in this case

plz explain me with this example.

Thanks & Regards,
Ravi kiran
Feb 20 '07 #1
2 1146
Ganon11
3,652 Expert 2GB
I wasn't sure what would happen, so I wrote a short test program to see:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void fun(int);
  5. void fun(long);
  6.  
  7. int main() {
  8.     cout << "testing overloaded functions" << endl;
  9.  
  10.     fun(10);
  11.  
  12.     system("pause");
  13.     return 0;
  14. }
  15.  
  16. void fun(int x) {
  17.     cout << "int fun() execution - x is " << x << endl;
  18. }
  19.  
  20. void fun(long x) {
  21.     cout << "long fun() execution - x is " << x << endl;
  22. }
During the run, the void fun(int x) function was executed instead of the long. Replacing the long with short (a.k.a. void fun(short x)), the int function still executed. This may be compiler specific, though - I used Bloodshed Dev C++ 4.9.9.2 to write this.
Feb 20 '07 #2
AdrianH
1,251 Expert 1GB
Hi,

I’ve looked up the grammar to C++ and found it here

Here is an excerpt:
Expand|Select|Wrap|Line Numbers
  1.     integer-suffix:
  2.         unsigned-suffix long-suffix_opt
  3.         long-suffix unsigned-suffix_opt
  4.  
  5.     unsigned-suffix: one of
  6.         u  U
  7.  
  8.     long-suffix: one of
  9.         l  L
  10.  
What this is saying is that an integer suffix (which is optional) can be “u”, “l”, “ul”, or “lu”. (opt means optional). So it looks like you cannot state a literal number that is of type short. If you want to use a short as a type for overloading, it will be beat out by other literal integer types unless you cast it.

Also, you could have tested this yourself and then ask "why does this happen?" May I ask why you didn't?


Adrian
Feb 20 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
2
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
9
by: Lawrence Krubner | last post by:
Possibly a dumb question but is type hinting allowed with overloaded methods? Can one class have these two methods? public function doSelect(Criteria $c) { // some code here } public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
0
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...
0
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,...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.