473,382 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

what does const mean when it comes *after* the function definition

eg int myfunction () const;

I understand myfunction(const thevariable), eg myfunction won't change
the value of "thevariable." But what is the significance of placing
const after the () in

int myfunction () const;

thanks!

Oct 12 '05 #1
14 11204
For class member functions, it means the function will not change the
state of the class. For example, the get function of a get/set pair of
member functions probably doesn't change the state of the class.

The definition of state, of course, can be more complicated than it
at first appears, given the mutable keyword for class data members.
But that's another question :).

Oct 12 '05 #2
alexl wrote:
eg int myfunction () const;

I understand myfunction(const thevariable), eg myfunction won't change
the value of "thevariable." But what is the significance of placing
const after the () in

int myfunction () const;


The object doesn't change in that non-static member function. IOW,
'this' points to a constant object.

V
Oct 12 '05 #3
alexl wrote:
e.g.

int myfunction(void) const;

I understand myfunction(const thevariable),
e.g. myfunction won't change the value of "thevariable."
But what is the significance of placing const after the () in

int myfunction(void) const; cat myfunction.cc int myfunction(void) const;
g++ -Wall -ansi -pedantic -c myfunction.cc myfunction.cc:1: error: non-member function ‘int myfunction()’ \
cannot have cv-qualifier

You probably meant:
cat myclass.cc

class myClass {
private:
// representation
int I;
public:
int myfunction(void) const;
};
Oct 12 '05 #4
Victor Bazarov wrote:
alexl wrote:
eg int myfunction () const;

I understand myfunction(const thevariable), eg myfunction won't change
the value of "thevariable." But what is the significance of placing
const after the () in

int myfunction () const;


The object doesn't change in that non-static member function. IOW,
'this' points to a constant object.

V

....And hence this function can be called on a const object.
Oct 12 '05 #5

An**********@gmail.com wrote:
For class member functions, it means the function will not change the
state of the class. For example, the get function of a get/set pair of
member functions probably doesn't change the state of the class.

The definition of state, of course, can be more complicated than it
at first appears, given the mutable keyword for class data members.
But that's another question :).


Please do not use the broken 'Reply' link at the bottom of a message.
Instead whenever you have to reply, please click on 'Show options' link
next to the subject of the post and from there click on 'Reply'.

Oct 12 '05 #6

Jaspreet wrote:
An**********@gmail.com wrote:
For class member functions, it means the function will not change the
state of the class. For example, the get function of a get/set pair of
member functions probably doesn't change the state of the class.

The definition of state, of course, can be more complicated than it
at first appears, given the mutable keyword for class data members.
But that's another question :).


Please do not use the broken 'Reply' link at the bottom of a message.
Instead whenever you have to reply, please click on 'Show options' link
next to the subject of the post and from there click on 'Reply'.


<OT but within the scope of netiquette I think>

I assume you're talking about posting from Google Groups. I've seen
this advice before. What is broken about the 'Reply' link at the bottom
of the page? I used that link to post to the recent 'namespace std and
standard headers' thread and didn't upset anyone. Am I missing
something?

Thanks
Gavin Deane

Oct 12 '05 #7
I am posting this time by clicking on the broken link to show its
actually broken.

Oct 13 '05 #8

Jaspreet wrote:
I am posting this time by clicking on the broken link to show its
actually broken.


Hi Gavin

I made the previous post by clicking on the Reply button from google
groups to show that its broken. I clicked on Reply at the end of your
message and look I never got the previous context.

I am actually not sure how you can make the post by clicking on Reply
and still have the previous context.

Thanks
Jaspreet

Oct 13 '05 #9
> I am actually not sure how you can make the post by clicking on Reply
and still have the previous context.
Thanks Jaspreet

Did you tried magical Copy+Paste functionality? (Like I just did)

Oct 13 '05 #10

Jaspreet wrote:
Jaspreet wrote:
I am posting this time by clicking on the broken link to show its
actually broken.


Hi Gavin

I made the previous post by clicking on the Reply button from google
groups to show that its broken. I clicked on Reply at the end of your
message and look I never got the previous context.

I am actually not sure how you can make the post by clicking on Reply
and still have the previous context.


Hmmmm ...

I posted this using the 'broken' Reply link and Google gave me the
previous context. Dunno what I'm doing differently, but as long as
losing the previous context is the only problem people are worried
about I'll make sure I don't do it.

Gavin Deane

Oct 13 '05 #11
In article <11**********************@g14g2000cwa.googlegroups .com>,
alexl <al**********@gmail.com> wrote:
eg int myfunction () const;

I understand myfunction(const thevariable), eg myfunction won't change
the value of "thevariable." But what is the significance of placing
const after the () in

int myfunction () const;


Check out http://www.comeaucomputing.com/techtalk/#constmf
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 13 '05 #12

ma************@gmail.com wrote:
I am actually not sure how you can make the post by clicking on Reply
and still have the previous context.
Thanks Jaspreet

Did you tried magical Copy+Paste functionality? (Like I just did)


You would not need to use the magic of copy-paste if you use the Reply
link at the message header rather than using the Reply link at the
bottom of the page. However, as long as you get the previous context we
all are happy.

Thanks.

Oct 13 '05 #13
ma************@gmail.com wrote:
I am actually not sure how you can make the post by clicking on
Reply and still have the previous context.
Thanks Jaspreet

Did you tried magical Copy+Paste functionality? (Like I just did)


Where are the attributions?
Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Oct 13 '05 #14
de*********@hotmail.com wrote:

I posted this using the 'broken' Reply link and Google gave me the
previous context. Dunno what I'm doing differently, but as long as
losing the previous context is the only problem people are worried
about I'll make sure I don't do it.


There is a way to do it from the regular Reply, clicking Preview or
something like that. However, it's generally easier to use the method
in my .sig. I know, I had to post from Google for four months earlier
this year.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Oct 13 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
4
by: Roy Yao | last post by:
Why the following code let my compiler complain an overloaded function Init()? // code begin template<class T> class BicircularList { template<class T> class Iterator; template<class T> class...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
15
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I'm trying to initialize an array of error messages, so that I can print out an error message by using the 'nth string in an array, e.g. printf("%s\n", messages); I'm still hazy on arrays of...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.