function signatures const vs. non const

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

    function signatures const vs. non const

    Hello team,

    C++ allows declarations of the fillowing form inside a class, which is
    very confusing

    void foo();
    void foo() const;

    My question is when will the second foo() be called as against first
    foo()

    Similarly

    int bar();

    const int bar();

    Which function will be executed as a result of a call to bar() and
    under what circumstances.

    Thanks.

  • Ian Collins

    #2
    Re: function signatures const vs. non const

    arun wrote:[color=blue]
    > Hello team,
    >
    > C++ allows declarations of the fillowing form inside a class, which is
    > very confusing
    >
    > void foo();
    > void foo() const;
    >
    > My question is when will the second foo() be called as against first
    > foo()
    >[/color]
    The const form will be called if the object is const, the non-const
    otherwise.

    --
    Ian Collins.

    Comment

    • Ian Collins

      #3
      Re: function signatures const vs. non const

      arun wrote:[color=blue]
      > Similarly
      >
      > int bar();
      >
      > const int bar();
      >
      > Which function will be executed as a result of a call to bar() and
      > under what circumstances.
      >[/color]
      I forgot this bit, you can't overload a const and non-const return with
      the same function signature.

      --
      Ian Collins.

      Comment

      • Rolf Magnus

        #4
        Re: function signatures const vs. non const

        arun wrote:
        [color=blue]
        > Hello team,
        >
        > C++ allows declarations of the fillowing form inside a class, which is
        > very confusing[/color]

        Only if you don't know what the 'const' means in this place.
        [color=blue]
        > void foo();
        > void foo() const;
        >
        > My question is when will the second foo() be called as against first
        > foo()[/color]

        The second will be called if the object it is called for is const or
        accessed through a reference or pointer to const. Otheriwse, the first one
        is chosen.
        [color=blue]
        > Similarly
        >
        > int bar();
        >
        > const int bar();
        >
        > Which function will be executed as a result of a call to bar() and
        > under what circumstances.[/color]

        This shouldn't compile, because the functions have the same signature.

        Comment

        • benben

          #5
          Re: function signatures const vs. non const

          arun wrote:[color=blue]
          > Hello team,
          >
          > C++ allows declarations of the fillowing form inside a class, which is
          > very confusing
          >
          > void foo();
          > void foo() const;
          >
          > My question is when will the second foo() be called as against first
          > foo()[/color]

          class Circle
          {
          double radius;
          public:
          Circle(double r):radius(r){}

          double& r(){return radius;}
          const double& r() const{return radius;}
          };

          int main()
          {
          Circle c1(10);
          const Circle c2(20);

          // You should be able to read
          // a const circle's radius:

          double r1 = c1.r(); // calls Circle::r()
          double r2 = c2.r(); // calls Circle::r() const

          // But not to write to:

          c1.r() = 3.5; // OK, writing to a double&
          c2.r() = 3.5; // ERROR, cannot write to a const double&
          }
          [color=blue]
          >
          > Similarly
          >
          > int bar();
          >
          > const int bar();
          >
          > Which function will be executed as a result of a call to bar() and
          > under what circumstances.[/color]

          Under no circumstances because this won't compile.
          [color=blue]
          >
          > Thanks.
          >[/color]

          Regards,
          Ben

          Comment

          Working...