#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0

    it is possible to overload return vals??


    anyone plz tell me
    it is possible to overload return vals??
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    Yes.
    Code:
    #include <iostream>
    using namespace std;
    
    int foo(int x) {
      return x + 1;
    }
    
    double foo(double x) {
      return x + 1;
    }
    
    int main(void) {
      int i = foo(1);
      double d = foo(2.0);
    
      cout << i << ", " << d << endl;
      return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    But Scorpions, isn't it still the data types in the parameter list and not the return type that do the overloading -- that makes that a unique function and enables the compiler to determine precisely which function is being called?

    For example, this should not work:
    Code:
    int foo(int x);
    double foo(int x);
    The compiler won't be able to tell the two apart. The return type won't help because that can be changed through automatic type casting or even through explicit type-casting. It still must be the parameter list that distinguishes one overloaded function from the other.

    Though I hadn't given any thought to the fact that overloaded functions could return different data types, so that is good to know.
    Last edited by dwise1_aol; July 20th, 2003 at 01:23 PM.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    Yeah, I know the parameter list determines the overload. I assumed that he was asking if the functions could return different data types. That's what I was demonstrating :).
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    Thank you Guys!

    plz see this
    i've three functions returns three diff vals
    can i declare like this?
    Code:
    int aton(char *);
    long aton(char *);
    double aton(char *);
    
    int main()
    {
        int i = aton("123");
        long l = aton("70000");
        double d = aton("12345678901.12334");
        return 0;
    }
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    No you can't, but you can declare them like this:
    Code:
    void aton(char *, int &);
    void aton(char *, long &);
    void aton(char *, double &);
    or
    Code:
    void aton(char *, int *);
    void aton(char *, long *);
    void aton(char *, double *);
    or even
    Code:
    int aton(char *, int *);
    long aton(char *, long *);
    double aton(char *, double *);
    Your choice :)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    Originally posted by MarkMan
    Thank you Guys!

    plz see this
    i've three functions returns three diff vals
    can i declare like this?
    Code:
    int aton(char *);
    long aton(char *);
    double aton(char *);
    
    int main()
    {
        int i = aton("123");
        long l = aton("70000");
        double d = aton("12345678901.12334");
        return 0;
    }
    As Scorpions says, that won't work. Read my discussion above for the reason why. Also read your reference's discussion of function overloading and what the requirements and restrictions are.

    The bottom line is that the compiler must know unequivocably which function you are calling and it can only distinguish between overloaded functions if their parameter lists are different. Yes, different overloaded functions can return different data types, but their parameter lists still need to be different in order to allow that overloading.

    From Bruce Eckel's "Thinking in C++", 2nd Ed, at http://www-h.eng.cam.ac.uk/help/tpl/...+/tic0083.html :
    Overloading on return values

    It’s common to wonder “why just scopes and argument lists? Why not return values?” It seems at first that it would make sense to also mangle the return value with the internal function name. Then you could overload on return values, as well:

    void f();
    int f();

    This works fine when the compiler can unequivocally determine the meaning from the context, as in int x = f( ); . However, in C you’ve always been able to call a function and ignore the return value. How can the compiler distinguish which call is meant in this case? Possibly worse is the difficulty the reader has in knowing which function call is meant. Overloading solely on return value is a bit too subtle, and thus isn’t allowed in C++.
    Please also note that overloading also restricts the use of default argument values.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    thank you Scorpions4ever & Dwise1_aol
    thats better to know this things
    once again thanks for clearing my doubt against overloading functions

IMN logo majestic logo threadwatch logo seochat tools logo