Overloading a function in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marko.Cain.23@gmail.com

    #1

    Overloading a function in C++

    Why the following code compiles in C++, it is overloading a funciton
    "x" with 2 different return value (one is int, one is void). Is that
    legal? Thanks.

    class Test1
    {
    public:
    Test1();
    virtual ~Test1();

    int x() { return _x;}
    void x(int x1) { _x = x1;}

    private:
    int _x;
    };

  • Sharad Kala

    #2
    Re: Overloading a function in C++


    <Marko.Cain.23@ gmail.com> wrote in message
    | Why the following code compiles in C++, it is overloading a funciton
    | "x" with 2 different return value (one is int, one is void). Is that
    | legal? Thanks.

    You are mistaken. It is not overloading on the return value, in fact you
    can't overload on the return value.

    | class Test1
    | {
    | public:
    | Test1();
    | virtual ~Test1();
    |
    | int x() { return _x;}

    This takes void as parameter and returns int

    | void x(int x1) { _x = x1;}

    This takes int as parameter and returns void

    |
    | private:
    | int _x;
    | };

    Sharad


    Comment

    • Jim Langston

      #3
      Re: Overloading a function in C++

      <Marko.Cain.23@ gmail.com> wrote in message
      news:1139898359 .036646.249590@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > Why the following code compiles in C++, it is overloading a funciton
      > "x" with 2 different return value (one is int, one is void). Is that
      > legal? Thanks.
      >
      > class Test1
      > {
      > public:
      > Test1();
      > virtual ~Test1();
      >
      > int x() { return _x;}
      > void x(int x1) { _x = x1;}
      >
      > private:
      > int _x;
      > };[/color]

      As long as the parameters are different, and in this case one is void and
      one is int, it is legal.


      Comment

      • Marco Wahl

        #4
        Re: Overloading a function in C++

        Marko.Cain.23@g mail.com writes:
        [color=blue]
        > class Test1
        > {
        > public:
        > Test1();
        > virtual ~Test1();
        >
        > int x() { return _x;}
        > void x(int x1) { _x = x1;}
        >
        > private:
        > int _x;
        > };[/color]
        [color=blue]
        > [...] Is that legal?[/color]

        Yes.

        Just to make it clear. The following class is illegal in C++.

        [[[
        /**
        This code will not compile.

        Cause: Two function-declarations that differ only in the
        return-type.
        */
        class Test2
        {
        public:
        int x();
        void x();
        };
        ]]]

        Different return-types can not be used for overloading functions in
        C++. Overloading is based on the arguments of functions.

        Comment

        • Tim Slattery

          #5
          Re: Overloading a function in C++

          Marko.Cain.23@g mail.com wrote:
          [color=blue]
          >Why the following code compiles in C++, it is overloading a funciton
          >"x" with 2 different return value (one is int, one is void). Is that
          >legal? Thanks.[/color]
          [color=blue]
          > int x() { return _x;}
          > void x(int x1) { _x = x1;}[/color]

          They can't differ *only* by return type. Since these two functions
          have different argument lists, your code will work fine.

          --
          Tim Slattery
          Slattery_T@bls. gov

          Comment

          Working...