By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,414 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Overloaded function

P: 1
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
Share this Question
Share on Google+
2 Replies


Ganon11
Expert 2.5K+
P: 3,652
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
Expert 100+
P: 1,251
Hi,

Ive 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

Post your reply

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