constant member functions of a class

B

blueblueblue2005

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
 
S

Sharad Kala

blueblueblue2005 said:
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
 
J

Jaspreet

blueblueblue2005 said:
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.
 
B

blueblueblue2005

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top