constant member functions of a class

Discussion in 'C++' started by blueblueblue2005, Jun 30, 2005.

  1. Hi, what is the different between the following two ways to use const
    in a function definition:

    1. const Array &operator=(const Array &);
    2. int getSize() const;

    keyword const to the left of return type and to the right of the
    parameter list, what is the different here? I know for the 2nd case,
    the function can not modify the object's data member.

    Thanks a lot

    -Holly
    blueblueblue2005, Jun 30, 2005
    #1
    1. Advertising

  2. blueblueblue2005

    Sharad Kala Guest

    "blueblueblue2005" <> wrote in message
    > Hi, what is the different between the following two ways to use const
    > in a function definition:


    These are not function definitions; they are declarations.

    >
    > 1. const Array &operator=(const Array &);


    The assignment operator takes a reference to const Array object and returns
    a reference to const Array object too.

    > 2. int getSize() const;


    getSize takes no parameters and returns an int. It promises to not mutate
    the object on which this member function has been called.

    Sharad
    Sharad Kala, Jun 30, 2005
    #2
    1. Advertising

  3. blueblueblue2005

    Jaspreet Guest

    blueblueblue2005 wrote:
    > Hi, what is the different between the following two ways to use const
    > in a function definition:


    Both below statements are function declarations and not definitions.
    Definition is when you write code in the function.

    >
    > 1. const Array &operator=(const Array &);

    Function returns a ref to a constant array.

    > 2. int getSize() const;

    If declared inside a class this means it cannot modify the values of
    the class data members. I guess this can be over-ridden if you decl a
    data member as mutable. Not sure though.

    >
    > keyword const to the left of return type and to the right of the
    > parameter list, what is the different here? I know for the 2nd case,
    > the function can not modify the object's data member.
    >
    > Thanks a lot
    >
    > -Holly
    Jaspreet, Jun 30, 2005
    #3
  4. they are declarations, sorry for the confusion, I just saved typing the
    body of the function definition. and they are the member function of a
    class. Thanks a lot, now I know the difference, one can not modify the
    object itself, the other one can modify the object, but return a const
    data
    blueblueblue2005, Jun 30, 2005
    #4
    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. E11
    Replies:
    1
    Views:
    4,742
    Thomas Weidenfeller
    Oct 12, 2005
  2. srinivas reddy

    constant member functions

    srinivas reddy, Jul 14, 2003, in forum: C++
    Replies:
    2
    Views:
    404
    Andrey Tarasevich
    Jul 14, 2003
  3. Fraser Ross
    Replies:
    4
    Views:
    1,041
    Fraser Ross
    Aug 14, 2004
  4. Hicham Mouline
    Replies:
    0
    Views:
    432
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    414
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page