const functions

Discussion in 'C++' started by Pmb, May 24, 2004.

  1. Pmb

    Pmb Guest

    What does it meant when a function member of a class is declared as const?

    Thanks

    Pmb
    Pmb, May 24, 2004
    #1
    1. Advertising

  2. Pmb

    Sharad Kala Guest

    Sharad Kala, May 24, 2004
    #2
    1. Advertising

  3. "Pmb" <> wrote in message
    news:...
    > What does it meant when a function member of a class is declared as const?
    >


    That it does not modify the object on which it is called.

    john
    John Harrison, May 24, 2004
    #3
  4. Pmb wrote:
    >
    > What does it meant when a function member of a class is declared as const?
    >


    That the function is not going to change the state of the
    object when called.

    In practice this means: This function can be called on const objects.


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 24, 2004
    #4
  5. Pmb

    Pmb Guest

    "Sharad Kala" <> wrote in message
    news:...
    >
    > "Pmb" <> wrote in message
    > news:...
    > > What does it meant when a function member of a class is declared as

    const?
    >
    > Always check the FAQ first.
    > http://www.parashift.com/c -faq-lite/const-correctness.html#faq-18.10


    Thanks. I checked the FAQ and didn't see that. I searched the page for
    "const function" and didn't see it.

    In any case, after reading it, I don't understand what it means by "The
    'abstract (client-visible) state of the object isn't going to change"

    Thanks

    Pmb
    Pmb, May 24, 2004
    #5
  6. Pmb wrote:
    >
    > "Sharad Kala" <> wrote in message
    > news:...
    > >
    > > "Pmb" <> wrote in message
    > > news:...
    > > > What does it meant when a function member of a class is declared as

    > const?
    > >
    > > Always check the FAQ first.
    > > http://www.parashift.com/c -faq-lite/const-correctness.html#faq-18.10

    >
    > Thanks. I checked the FAQ and didn't see that. I searched the page for
    > "const function" and didn't see it.
    >
    > In any case, after reading it, I don't understand what it means by "The
    > 'abstract (client-visible) state of the object isn't going to change"
    >


    Exactly what it says.

    There is a class.

    There is an object of this class.

    Now this object is used by some client code.

    The client code calls a member function of this object.

    When calling the member function, the client code will not
    notify any changes in the state of that object.

    Example:
    I write a class which models a person. The state of that person
    consists of its name and its birthdate.
    I add a member function to that class which allows you to get
    the birthdate. But by calling that function, the object will
    not change it's visible state: Neither will the name change
    nor will the birthdate change. Thus I will make that function
    a const one.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 24, 2004
    #6
  7. Pmb

    Pmb Guest

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    > Pmb wrote:
    > >
    > > What does it meant when a function member of a class is declared as

    const?
    > >

    >
    > That the function is not going to change the state of the
    > object when called.
    >
    > In practice this means: This function can be called on const objects.


    I don't understand!? Take the program below as an example. The output is

    ===
    Object Test constructed with x = 1, y = 2
    x in print() is 8
    y in print() is 9
    ===

    The object "test" was declared constant and yet I modified the two data
    members x, y. Does that mean that I've changed the value of the object or
    the state of the object.

    Perhaps I don't understand what is meant above by the "state of the object"?

    Thanks

    Pmb

    --------------------------------------------------------------------------
    #include <iostream.h>

    class Test{
    public:
    Test( int = 0, int = 0 );
    void print( int , int ) const;
    private:
    int x;
    int y;
    };

    Test::Test( int i , int j )
    {
    x = i;
    y = j;
    cout << "Object Test constructed with x = " << x << ", y = " << y <<
    endl;
    }

    void Test::print ( int x, int y ) const
    {
    cout << "x in print() is " << x << endl;
    cout << "y in print() is " << y << endl;
    }

    int main()
    {
    Test const test( 1, 2 );

    test.print ( 8, 9 );

    return 0;

    }
    ---------------------------------------------------------------------------
    Pmb, May 24, 2004
    #7
  8. Pmb wrote:
    >
    > "Karl Heinz Buchegger" <> wrote in message
    > news:...
    > > Pmb wrote:
    > > >
    > > > What does it meant when a function member of a class is declared as

    > const?
    > > >

    > >
    > > That the function is not going to change the state of the
    > > object when called.
    > >
    > > In practice this means: This function can be called on const objects.

    >
    > I don't understand!? Take the program below as an example. The output is
    >
    > ===
    > Object Test constructed with x = 1, y = 2
    > x in print() is 8
    > y in print() is 9
    > ===
    >
    > The object "test" was declared constant and yet I modified the two data
    > members x, y.


    Where?
    You didn't

    In the print function you printed the values passed
    to that function, not the member variables.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 24, 2004
    #8
  9. "Pmb" <> wrote in message
    news:...
    >
    > "Karl Heinz Buchegger" <> wrote in message
    > news:...
    > > Pmb wrote:
    > > >
    > > > What does it meant when a function member of a class is declared as

    > const?
    > > >

    > >
    > > That the function is not going to change the state of the
    > > object when called.
    > >
    > > In practice this means: This function can be called on const objects.

    >
    > I don't understand!? Take the program below as an example. The output is
    >
    > ===
    > Object Test constructed with x = 1, y = 2
    > x in print() is 8
    > y in print() is 9
    > ===
    >
    > The object "test" was declared constant and yet I modified the two data
    > members x, y.


    No you didn't. In print x and y are parameters they are not the member
    variables x and y. You wouldnot be able to change the member variables x and
    y, but you can change the parameters x and y because they are not declared
    const.

    > Does that mean that I've changed the value of the object or
    > the state of the object.


    No you haven't.

    >
    > Perhaps I don't understand what is meant above by the "state of the

    object"?

    I think you do, the problem it that you don't see that you have two
    different x's and two different y's in your program.

    john
    John Harrison, May 24, 2004
    #9
  10. Pmb

    Sharad Kala Guest

    "Pmb" <> wrote in message
    news:...
    >

    [snip]
    > In any case, after reading it, I don't understand what it means by "The
    > 'abstract (client-visible) state of the object isn't going to change"


    Say you have a class that contains a char pointer as a member. You allocate
    memory for that in the constructor. Later in a member function you only change
    the contents of the allocated memory . Now as far as client is concerned there
    is constness (no change in value of p though what it points to has changed),
    hence it can be declared const.

    -Sharad
    Sharad Kala, May 24, 2004
    #10
  11. Pmb

    Pmb Guest

    "John Harrison" <> wrote in message
    news:...
    >
    > "Pmb" <> wrote in message
    > news:...
    > >
    > > "Karl Heinz Buchegger" <> wrote in message
    > > news:...
    > > > Pmb wrote:
    > > > >
    > > > > What does it meant when a function member of a class is declared as

    > > const?
    > > > >
    > > >
    > > > That the function is not going to change the state of the
    > > > object when called.
    > > >
    > > > In practice this means: This function can be called on const objects.

    > >
    > > I don't understand!? Take the program below as an example. The output is
    > >
    > > ===
    > > Object Test constructed with x = 1, y = 2
    > > x in print() is 8
    > > y in print() is 9
    > > ===
    > >
    > > The object "test" was declared constant and yet I modified the two data
    > > members x, y.

    >
    > No you didn't. In print x and y are parameters they are not the member
    > variables x and y. You wouldnot be able to change the member variables x

    and
    > y, but you can change the parameters x and y because they are not declared
    > const.


    Oops! Thanks

    Pmb
    Pmb, May 24, 2004
    #11
  12. Pmb

    Simon Guest

    Looking at your code bellow

    >
    > int main()
    > {
    > Test const test( 1, 2 );


    You made the values of x and y equal to 1 and 2.
    But not any x and y, the x and y that belongs to the class Test.
    To do that you used to values i and j

    >
    > test.print ( 8, 9 );


    now your function prints out two values that you have passed 8 and 9 but to
    pass the values you called them x and y in your function.
    The fact that the class Test also has two place holders called x and y is
    irrelevant really, (but bad programming).
    If you change your function to
    void 'Test::print ( int i, int j )' const the output will be the value of
    the x and y in the class Test, (and i and j would effectively be ignored).

    >
    > return 0;
    >
    > }
    > --------------------------------------------------------------------------

    -
    >


    As for a const function it is a function that does not assign values within
    it's body.
    if you did

    void Test::print ( int i, int j ) const
    {
    x = j;

    cout << "x in print() is " << x << endl;
    cout << "y in print() is " << y << endl;
    }

    it would not work because you are trying to change the value of x. That is
    not permitted in a const function.

    (note that i oversimplified what happens so you get a better idea of what is
    going on).
    Simon.
    Simon, May 24, 2004
    #12
  13. Pmb

    JKop Guest

    Pmb posted:

    > What does it meant when a function member of a class is declared as
    > const?
    >
    > Thanks
    >
    > Pmb
    >
    >



    int DoStuff(const Dog& doggie)
    {
    doggie.age = 5;

    //ERROR, CANNOT EDIT const OBJECT

    return 0;

    }


    -JKop
    JKop, May 24, 2004
    #13
  14. Pmb

    Sharad Kala Guest

    "JKop" <> wrote in message
    news:Nulsc.253$...
    > Pmb posted:
    >
    > > What does it meant when a function member of a class is declared as
    > > const?
    > >


    > int DoStuff(const Dog& doggie)
    > {
    > doggie.age = 5;
    >
    > //ERROR, CANNOT EDIT const OBJECT
    >
    > return 0;
    >
    > }


    His question is that what is a const member function if you read carefully.
    Sharad Kala, May 24, 2004
    #14
  15. Pmb

    JKop Guest

    Sharad Kala posted:

    > His question is that what is a const member function if you read
    > carefully.



    Opps!!!



    int Dog::DoStuff(void) const
    {
    age = 5;

    //ERROR, CANNOT EDIT OBJECT, THIS IS A const FUNCTION!!

    return 0;
    }



    Checklist:

    1) Does your function edit object member variables? If not, declare it
    const .

    3) Does your function neither read nor edit member variables? If so, declare
    it static .


    -JKop
    JKop, May 24, 2004
    #15
  16. JKop wrote:
    > ...
    > Checklist:
    >
    > 1) Does your function edit object member variables? If not, declare it
    > const .


    Not correct. For example, take a look at non-const version of
    'std::vector::eek:perator[]'. It doesn't edit any of the object's member
    variables. Do you think it should've been declared as 'const'? What
    about non-const versions of 'begin()' and 'end()' methods in standard
    containers?

    > 3) Does your function neither read nor edit member variables? If so, declare
    > it static .


    Not exactly correct either (for similar reasons).

    BTW, where is 2) ?

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, May 24, 2004
    #16
    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,091
  2. Javier
    Replies:
    2
    Views:
    549
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,095
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    874
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    531
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page