const method question

Discussion in 'C++' started by scooter, Oct 28, 2003.

  1. scooter

    scooter Guest

    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()const
    {
    //do some calculations

    return some_value;
    }
    };
    scooter, Oct 28, 2003
    #1
    1. Advertising

  2. "scooter" <> wrote in message
    news:...
    > 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()const
    > {
    > //do some calculations
    >
    > return some_value;
    > }
    > };


    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
    Josephine Schafer, Oct 28, 2003
    #2
    1. Advertising

  3. 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>
    > 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.

    <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.
    Stewart Gordon, Oct 28, 2003
    #3
  4. "Stewart Gordon" <> wrote in message
    news:bnlpgq$51h$...
    >
    >
    > 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:


    Sprinkling some more black dots........:)

    > <snip>
    > > 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.

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


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

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

    HTH,
    J.Schafer
    Josephine Schafer, Oct 28, 2003
    #4
  5. scooter

    jeffc Guest

    "scooter" <> wrote in message
    news:...
    > 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()const
    > {
    > //do some calculations
    >
    > return some_value;
    > }
    > };


    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.
    jeffc, Oct 28, 2003
    #5
  6. scooter

    chris Guest

    Josephine Schafer wrote:

    > "Stewart Gordon" <> wrote in message
    > news:bnlpgq$51h$...
    >
    >>
    >>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:

    >
    >
    > Sprinkling some more black dots........:)
    >
    >
    >><snip>
    >>
    >>>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.

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

    >
    >
    > Ofcourse.
    > [To the OP] const qualifier can be used for function overloading.
    >
    > Overloads -
    > void foo () const{}
    > void foo (){}
    >
    > HTH,
    > J.Schafer
    >
    >

    although you really won't make friends if 'foo() const' and 'foo()' do
    different things! :)
    chris, Oct 28, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    11
    Views:
    1,101
  2. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,115
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    887
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    541
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page