const method question

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

    const method question

    Is there any advantage in using the second form?

    thanks



    //-------------------------------1st
    class MyClass
    {
    int a;

    DoSomething()
    {
    //do some calculations

    Calculate();
    }

    Calculate()
    {
    //do some calculations

    a = some_value;
    }
    };


    //-------------------------------2nd
    class MyClass
    {
    int a;

    DoSomething()
    {
    //do some calculations

    a = Calculate();
    }

    int Calculate()cons t
    {
    //do some calculations

    return some_value;
    }
    };
  • Josephine Schafer

    #2
    Re: const method question


    "scooter" <scooter@btopen world.com> wrote in message
    news:t49spvof9a 5825k4rc30qbi1n 0cfukq9lk@4ax.c om...[color=blue]
    > Is there any advantage in using the second form?
    >
    > thanks
    >
    >
    >
    > //-------------------------------1st
    > class MyClass
    > {
    > int a;
    >
    > DoSomething()
    > {
    > //do some calculations
    >
    > Calculate();
    > }
    >
    > Calculate()
    > {
    > //do some calculations
    >
    > a = some_value;
    > }
    > };
    >
    >
    > //-------------------------------2nd
    > class MyClass
    > {
    > int a;
    >
    > DoSomething()
    > {
    > //do some calculations
    >
    > a = Calculate();
    > }
    >
    > int Calculate()cons t
    > {
    > //do some calculations
    >
    > return some_value;
    > }
    > };[/color]

    const means that the member function does not alter the state of the object.
    So if your function doesn't change the object's state then declare it const.
    Declaring const has an indirect advantage that if you inadvertently try to
    mutate object state through a
    const function then compiler stops you. Also for debugging purposes you know for
    sure that these functions aren't the culprit functions which could have
    modified the object state.

    HTH,
    J.Schafer


    Comment

    • Stewart Gordon

      #3
      Re: const method question



      While it was 28/10/03 8:50 am throughout the UK, Josephine Schafer
      sprinkled little black dots on a white screen, and they fell thus:

      <snip>[color=blue]
      > const means that the member function does not alter the state of the object.
      > So if your function doesn't change the object's state then declare it const.
      > Declaring const has an indirect advantage that if you inadvertently try to
      > mutate object state through a const function then compiler stops you.[/color]
      <snip>

      There's also the direct advantage that you can call it on a const object.

      Stewart.

      --
      My e-mail is valid but not my primary mailbox. Please keep replies on
      on the 'group where everyone may benefit.

      Comment

      • Josephine Schafer

        #4
        Re: const method question


        "Stewart Gordon" <smjg_1998@yaho o.com> wrote in message
        news:bnlpgq$51h $2@sun-cc204.lut.ac.uk ...[color=blue]
        >
        >
        > While it was 28/10/03 8:50 am throughout the UK, Josephine Schafer
        > sprinkled little black dots on a white screen, and they fell thus:[/color]

        Sprinkling some more black dots........:-)
        [color=blue]
        > <snip>[color=green]
        > > const means that the member function does not alter the state of the object.
        > > So if your function doesn't change the object's state then declare it const.
        > > Declaring const has an indirect advantage that if you inadvertently try to
        > > mutate object state through a const function then compiler stops you.[/color]
        > <snip>
        >
        > There's also the direct advantage that you can call it on a const object.
        >[/color]

        Ofcourse.
        [To the OP] const qualifier can be used for function overloading.

        Overloads -
        void foo () const{}
        void foo (){}

        HTH,
        J.Schafer



        Comment

        • jeffc

          #5
          Re: const method question


          "scooter" <scooter@btopen world.com> wrote in message
          news:t49spvof9a 5825k4rc30qbi1n 0cfukq9lk@4ax.c om...[color=blue]
          > Is there any advantage in using the second form?
          >
          > thanks
          >
          >
          >
          > //-------------------------------1st
          > class MyClass
          > {
          > int a;
          >
          > DoSomething()
          > {
          > //do some calculations
          >
          > Calculate();
          > }
          >
          > Calculate()
          > {
          > //do some calculations
          >
          > a = some_value;
          > }
          > };
          >
          >
          > //-------------------------------2nd
          > class MyClass
          > {
          > int a;
          >
          > DoSomething()
          > {
          > //do some calculations
          >
          > a = Calculate();
          > }
          >
          > int Calculate()cons t
          > {
          > //do some calculations
          >
          > return some_value;
          > }
          > };[/color]

          I'd prefer the second. That Calculate function can be called from other
          const functions, while the first one can't. The name of the first function
          DoSomething implies that it's the one that will actually be making some
          change. The second function just figures it out. It seems to me that there
          is lower "coupling" and higher "cohesion" in the second example, if you'd
          like to look up those terms.


          Comment

          • chris

            #6
            Re: const method question

            Josephine Schafer wrote:
            [color=blue]
            > "Stewart Gordon" <smjg_1998@yaho o.com> wrote in message
            > news:bnlpgq$51h $2@sun-cc204.lut.ac.uk ...
            >[color=green]
            >>
            >>While it was 28/10/03 8:50 am throughout the UK, Josephine Schafer
            >>sprinkled little black dots on a white screen, and they fell thus:[/color]
            >
            >
            > Sprinkling some more black dots........:-)
            >
            >[color=green]
            >><snip>
            >>[color=darkred]
            >>>const means that the member function does not alter the state of the object.
            >>>So if your function doesn't change the object's state then declare it const.
            >>>Declaring const has an indirect advantage that if you inadvertently try to
            >>>mutate object state through a const function then compiler stops you.[/color]
            >>
            >><snip>
            >>
            >>There's also the direct advantage that you can call it on a const object.
            >>[/color]
            >
            >
            > Ofcourse.
            > [To the OP] const qualifier can be used for function overloading.
            >
            > Overloads -
            > void foo () const{}
            > void foo (){}
            >
            > HTH,
            > J.Schafer
            >
            >[/color]
            although you really won't make friends if 'foo() const' and 'foo()' do
            different things! :)

            Comment

            Working...