Can we overload on return types

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Panks

    Can we overload on return types

    Hey All

    Its one of basic questions? i read in a book "Thinking in C++" it says
    " Overloading solely on return value is a bit
    too subtle, and thus isn't allowed in C++." on page 323. While i
    tried overloading on VC++ compiler i was able to overload on return
    types.

    Please comment


    Pankaj

  • Victor Bazarov

    #2
    Re: Can we overload on return types

    Panks wrote:
    Its one of basic questions? i read in a book "Thinking in C++" it says
    " Overloading solely on return value is a bit
    too subtle, and thus isn't allowed in C++." on page 323. While i
    tried overloading on VC++ compiler i was able to overload on return
    types.
    Please give us the example of your success. Or did you mistakenly
    omit the "not" between "was" and "able"?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • sharmaharish@gmail.com

      #3
      Re: Can we overload on return types

      Pankaj,

      I don't think that is allowed by C++ language or by any of its
      conforming compilers. TICPP mentions this correctly. However, there is
      a way by which you can get similar behaviour, by using templates. eg.

      template <class T>
      T overloaded_on_r eturn_type()
      {
      T var;
      cout << "\n" << var;
      return var;
      }

      The type used to call the template function has to be either an
      intrinsic type or it should be some UDT that has the insertion operator
      overloaded for it (atleast for this example).

      Calling this function is as simple as
      'overloaded_on_ return_type<int >();'.

      Regards,
      Harish Sharma

      Comment

      • Pete Becker

        #4
        Re: Can we overload on return types

        sharmaharish@gm ail.com wrote:
        >
        Calling this function is as simple as
        'overloaded_on_ return_type<int >();'.
        >
        Despite the name, that's not overloading.

        Comment

        • Panks

          #5
          Re: Can we overload on return types

          Thanks Victor i didnt miss any don'ts in my post.

          Here is the code snippet and it works wen u compile with VC++ 6.0
          compiler.


          struct a {
          int i;
          };

          void fun1(int i)
          {
          printf("\n Hello void ret");
          }

          float fun1(int i,int j)
          {
          printf("\n Hello float ret");
          return 4.5;
          }

          a fun1(int i,double p)
          {
          a op;
          printf("\n Hello UDD ret");
          return op;
          }

          int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
          {
          int nRetCode = 0;
          fun1(10);
          fun1(10,10);
          fun1(10,10.0f);

          return nRetCode;
          }

          Hope for QuickReply
          Pankaj

          Victor Bazarov wrote:
          Panks wrote:
          Its one of basic questions? i read in a book "Thinking in C++" it says
          " Overloading solely on return value is a bit
          too subtle, and thus isn't allowed in C++." on page 323. While i
          tried overloading on VC++ compiler i was able to overload on return
          types.
          >
          Please give us the example of your success. Or did you mistakenly
          omit the "not" between "was" and "able"?
          >
          V
          --
          Please remove capital 'A's when replying by e-mail
          I do not respond to top-posted replies, please don't ask

          Comment

          • Victor Bazarov

            #6
            Re: Can we overload on return types

            Panks wrote:
            Thanks Victor i didnt miss any don'ts in my post.
            First of all, please don't top-post. Second, please don't quote sigs.
            If you don't feel the need in any of the contents of the post to which
            you're replying, please take time to remove it before hitting "send".

            Thanks.
            Here is the code snippet and it works wen u compile with VC++ 6.0
            compiler.
            Oh, I have no doubt of that. Except for the missing 'include' and
            the wrong definition of the 'main' function, it's normal C++ and should
            compile anywhere.
            >
            >
            struct a {
            int i;
            };
            >
            void fun1(int i)
            {
            printf("\n Hello void ret");
            }
            >
            float fun1(int i,int j)
            {
            printf("\n Hello float ret");
            return 4.5;
            }
            >
            a fun1(int i,double p)
            {
            a op;
            printf("\n Hello UDD ret");
            return op;
            }
            >
            int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
            {
            int nRetCode = 0;
            fun1(10);
            fun1(10,10);
            fun1(10,10.0f);
            So, could you explain to me why do you think that you're overloading
            *on* the return value type rather than on the number and the types of
            the arguments here? What *role* does the return value type play in
            the compiler's selecting the right function to call?
            >
            return nRetCode;
            }
            >
            Hope for QuickReply
            Pankaj
            >
            Victor Bazarov wrote:
            >Panks wrote:
            >>Its one of basic questions? i read in a book "Thinking in C++" it
            >>says " Overloading solely on return value is a bit
            >>too subtle, and thus isn't allowed in C++." on page 323. While i
            >>tried overloading on VC++ compiler i was able to overload on return
            >>types.
            >>
            >Please give us the example of your success. Or did you mistakenly
            >omit the "not" between "was" and "able"?
            >>
            >V
            >--
            >Please remove capital 'A's when replying by e-mail
            >I do not respond to top-posted replies, please don't ask
            V
            --
            Please remove capital 'A's when replying by e-mail
            I do not respond to top-posted replies, please don't ask


            Comment

            • Noah Roberts

              #7
              Re: Can we overload on return types


              Panks wrote:
              Hey All
              >
              Its one of basic questions? i read in a book "Thinking in C++" it says
              " Overloading solely on return value is a bit
              too subtle, and thus isn't allowed in C++." on page 323. While i
              tried overloading on VC++ compiler i was able to overload on return
              types.
              It is only possible when you are overriding a function in a base class
              with a return value that derives from the return of the target
              function:

              struct A {};
              struct B : A {};

              struct C
              {
              A * whatever();
              };

              struct D : C
              {
              B * whatever();
              };

              More commonly you use this to implement a polymorphic copy function.

              I know of no other condition that allows this...I am not sure if being
              a pointer or reference is a requirement though it wouldn't surprise me
              if it was.

              Comment

              • Panks

                #8
                Re: Can we overload on return types

                Hi

                I thought to remind you of post anyways. There is no foul play in
                original post a gave you strict statement from a books that overloading
                on return type is not supported.
                Ok if now if i am able to compile the code and run it hence compiler
                accepts this overloaded definition. No matter i have overloaded it on
                parameters also. You can add code to accept the return values it runs
                even that also error free.

                Point is why is the contradiction? Rather looking for header file
                stement i hope you focus on core issue.

                Pankaj

                Comment

                • Victor Bazarov

                  #9
                  Re: Can we overload on return types

                  Panks wrote:
                  I thought to remind you of post anyways. There is no foul play in
                  original post a gave you strict statement from a books that
                  overloading on return type is not supported.
                  Ok if now if i am able to compile the code and run it hence compiler
                  accepts this overloaded definition. No matter i have overloaded it on
                  parameters also. You can add code to accept the return values it runs
                  even that also error free.
                  >
                  Point is why is the contradiction? Rather looking for header file
                  stement i hope you focus on core issue.
                  I can barely understand what your point is. Let me explain what is
                  meant by "overloadin g on return value". If we remove all other function
                  signature differences (name, number and types of arguments), all that
                  remains is the return value type. Now, given that two functions *only*
                  differ in the return value type, is it allowed? If it were allowed, the
                  following would be legal code:

                  int foo();
                  double foo();

                  int main() {
                  return foo();
                  }

                  and the function 'foo' chosen in the 'main' function would be the one
                  that retuns an int. Now, since it _isn't_ allowed, the compiler will
                  refuse to compile this code.

                  In your example functions had different number and types of arguments.
                  And that's how the compiler knows which function to use. Return value
                  types play _no_role_ there. You can make them all the same, you can
                  make them different, it *does not matter*.

                  V
                  --
                  Please remove capital 'A's when replying by e-mail
                  I do not respond to top-posted replies, please don't ask


                  Comment

                  • Victor Bazarov

                    #10
                    Re: Can we overload on return types

                    Noah Roberts wrote:
                    Panks wrote:
                    >Hey All
                    >>
                    >Its one of basic questions? i read in a book "Thinking in C++" it
                    >says " Overloading solely on return value is a bit
                    >too subtle, and thus isn't allowed in C++." on page 323. While i
                    >tried overloading on VC++ compiler i was able to overload on return
                    >types.
                    >
                    It is only possible when you are overriding a function in a base class
                    with a return value that derives from the return of the target
                    function:
                    >
                    struct A {};
                    struct B : A {};
                    >
                    struct C
                    {
                    A * whatever();
                    };
                    >
                    struct D : C
                    {
                    B * whatever();
                    };
                    >
                    More commonly you use this to implement a polymorphic copy function.
                    >
                    I know of no other condition that allows this...I am not sure if being
                    a pointer or reference is a requirement though it wouldn't surprise me
                    if it was.
                    I am not sure why you gave this example. There is no *overloading* in
                    your code. None. Whatsoever.

                    The only time where return value types begin playing some role in the
                    function signature (and therefore in picking which function to use) is
                    in pointer-to-function conversions from a template. Observe:

                    template<class T,class UT foo(U) { return T(); }

                    int main() {
                    double (*p)(int) = foo;
                    }

                    Here, the template argument 'T' is deduced from the function type, and
                    that's the only place where it matters.

                    V
                    --
                    Please remove capital 'A's when replying by e-mail
                    I do not respond to top-posted replies, please don't ask


                    Comment

                    • Panks

                      #11
                      Re: Can we overload on return types

                      Thanks Harish

                      but pls below code and it executes wen compiled by VC++ compiler

                      struct a {
                      int i;
                      };

                      void fun1(int i)
                      {
                      printf("\n Hello void ret");
                      }

                      float fun1(int i,int j)
                      {
                      printf("\n Hello float ret");
                      return 4.5;
                      }

                      a fun1(int i,double p)
                      {
                      a op;
                      printf("\n Hello UDD ret");
                      return op;
                      }

                      int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                      {
                      int nRetCode = 0;
                      fun1(10);
                      float f=fun1(10,10);
                      a vara=fun1(10,10 .0f);
                      return nRetCode;
                      }


                      Its overloading right? Here along with parameter overloading is done
                      also on return types. Isnt this the contradiction of statement TICPP.
                      Although i accept overloading of type :
                      void fun();
                      int fun();

                      is not allowed. Please comment

                      Pankaj

                      Comment

                      • Panks

                        #12
                        Re: Can we overload on return types

                        Thanks Harish

                        but pls below code and it executes wen compiled by VC++ compiler

                        struct a {
                        int i;
                        };

                        void fun1(int i)
                        {
                        printf("\n Hello void ret");
                        }

                        float fun1(int i,int j)
                        {
                        printf("\n Hello float ret");
                        return 4.5;
                        }

                        a fun1(int i,double p)
                        {
                        a op;
                        printf("\n Hello UDD ret");
                        return op;
                        }

                        int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                        {
                        int nRetCode = 0;
                        fun1(10);
                        float f=fun1(10,10);
                        a vara=fun1(10,10 .0f);
                        return nRetCode;
                        }


                        Its overloading right? Here along with parameter overloading is done
                        also on return types. Isnt this the contradiction of statement TICPP.
                        Although i accept overloading of type :
                        void fun();
                        int fun();

                        is not allowed. Please comment

                        Pankaj

                        Comment

                        • Victor Bazarov

                          #13
                          Re: Can we overload on return types

                          Panks wrote:
                          Thanks Harish
                          >
                          but pls below code and it executes wen compiled by VC++ compiler
                          >
                          struct a {
                          int i;
                          };
                          >
                          void fun1(int i)
                          {
                          printf("\n Hello void ret");
                          }
                          >
                          float fun1(int i,int j)
                          {
                          printf("\n Hello float ret");
                          return 4.5;
                          }
                          >
                          a fun1(int i,double p)
                          {
                          a op;
                          printf("\n Hello UDD ret");
                          return op;
                          }
                          >
                          int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
                          This is not C++. Please when posting to comp.lang.c++ try to
                          remove any compiler-specific elements. Example: _tmain is MS,
                          and so is TCHAR. We here prefer this definition of 'main':

                          int main()

                          (no need for arguments which you aren't using).
                          {
                          int nRetCode = 0;
                          fun1(10);
                          float f=fun1(10,10);
                          a vara=fun1(10,10 .0f);
                          return nRetCode;
                          }
                          >
                          >
                          Its overloading right?
                          Yes.
                          Here along with parameter overloading is done
                          also on return types.
                          No. Return value types have *nothing* to do with the overloading
                          that you have happening there.
                          Isnt this the contradiction of statement TICPP.
                          Although i accept overloading of type :
                          void fun();
                          int fun();
                          >
                          is not allowed. Please comment
                          Done. And please don't post twice.

                          V
                          --
                          Please remove capital 'A's when replying by e-mail
                          I do not respond to top-posted replies, please don't ask


                          Comment

                          • shadowman615@comcast.net

                            #14
                            Re: Can we overload on return types


                            Panks wrote:
                            Its overloading right? Here along with parameter overloading is done
                            also on return types. Isnt this the contradiction of statement TICPP.
                            Although i accept overloading of type :
                            void fun();
                            int fun();
                            >
                            is not allowed. Please comment
                            >
                            Pankaj
                            No,. read the statement you quoted again (emphasis mine) :
                            "Overloadin g ***SOLELY*** on return value is a bit too subtle, and thus
                            isn't allowed in C++."

                            Comment

                            • Panks

                              #15
                              Re: Can we overload on return types

                              Victor Bazarov

                              Had enough of you. Pls dont reply to my post as i put up question for
                              Harish Sharma. Oversmartness is harmfull too health.

                              No need too reply.

                              Pankaj

                              Comment

                              Working...