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

A

alexl

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!
 
A

AnonMail2005

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 :).
 
V

Victor Bazarov

alexl said:
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
 
E

E. Robert Tisdale

alexl said:
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;
};
 
R

Ron Natalie

Victor said:
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.
 
J

Jaspreet

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'.
 
D

deane_gavin

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

Jaspreet

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

Jaspreet

Jaspreet said:
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
 
M

makc.the.great

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)
 
D

deane_gavin

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

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.
 
D

Default User

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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top