Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constant member functions of a class

Reply
Thread Tools

constant member functions of a class

 
 
blueblueblue2005
Guest
Posts: n/a
 
      06-30-2005
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

 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      06-30-2005

"blueblueblue2005" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Jaspreet
Guest
Posts: n/a
 
      06-30-2005
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


 
Reply With Quote
 
blueblueblue2005
Guest
Posts: n/a
 
      06-30-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading non-template member functions with template member functions Hicham Mouline C++ 1 04-24-2009 07:47 AM
overloading non-template member functions with template member functions Hicham Mouline C++ 0 04-23-2009 11:42 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
constant member functions srinivas reddy C++ 2 07-14-2003 10:52 PM



Advertisments