C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const function?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
franco ziade
Guest





PostPosted: Fri Feb 25, 2005 2:12 pm    Post subject: const function? Reply with quote



what does this declaration mean in C++

void func_name() const
{ ... }

is this a const function ?
what are the implications?

thanks


Back to top
Tim Love
Guest





PostPosted: Fri Feb 25, 2005 2:20 pm    Post subject: Re: const function? Reply with quote



"franco ziade" <fziade (AT) videotron (DOT) ca> writes:

Quote:
what does this declaration mean in C++

void func_name() const
{ ... }

is this a const function ?
what are the implications?

http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/argsC++.html
may help.

Back to top
Peter Koch Larsen
Guest





PostPosted: Fri Feb 25, 2005 2:34 pm    Post subject: Re: const function? Reply with quote




"franco ziade" <fziade (AT) videotron (DOT) ca> skrev i en meddelelse
news:6qGTd.48193$6U2.948612 (AT) weber (DOT) videotron.net...
Quote:
what does this declaration mean in C++

void func_name() const
{ ... }

is this a const function ?
what are the implications?

thanks


This does not compile. Probably from a textbook where "..." intends to mean

that the body is left out.

/Peter



Back to top
Gianni Mariani
Guest





PostPosted: Fri Feb 25, 2005 3:55 pm    Post subject: Re: const function? Reply with quote

franco ziade wrote:
Quote:
what does this declaration mean in C++

void func_name() const
{ ... }

is this a const function ?

It means that func_name() does not change *this;

You can think of the "this" pointer being an invisible paramter to the
function.

void func_name( T * this ) (no const)
void func_name( const T * this ) (const)

Quote:
what are the implications?

You can use this to discriminate between a const function call and a
non-const function call.

i.e.
struct A { void f () const; void f(); };

int main()
{
A a;
const A& ca = a;

a.f(); // calls void f()
ac.f();// calls void f () const;
}


Quote:

thanks



Back to top
Chris Jefferson
Guest





PostPosted: Fri Feb 25, 2005 4:57 pm    Post subject: Re: const function? Reply with quote

franco ziade wrote:
Quote:
what does this declaration mean in C++

void func_name() const
{ ... }


The const only really makes sense in member functions of classes. It
means that this class won't alter the state of the class. You should try
to mark all functions const which are const :)

For example, consider:

class S {
int i;
public:
int get() const { return i; }
void put(int j) { i=j; }
};

notice that I made get() const as it doesn't effect the state of the
class. Put is not const (and if you tried making it const it wouldn't
compile)

Now consider this function

void print(const S& s)
{ std::cout << s.get(); }

If get() wasn't const, this wouldn't compile, you you would be calling a
non-const member function on a const object.

One particular point of note is that you should be sure to make ==, !=,
<, and the other relational operators on classes const (assuming of
course they are), as many of the implementations of the standard
algorithms assume (and I believe they are allowed to assume) they can
put a const on things without effecting anything.

One final point. You can do:

public S
{
int i;
public:
int get() const {return i;}
int get() {i=0; return i;}
};

ie you can define the const and non-const version of a function
differently, and give them different meanings. You DON'T have to do this
just so non-const objects have a function (the const version can still
be used on non-const Ss).

Now different things will happen depending on is some instance of S is
const or not. Of course I've only ever seen someone write this once when
they weren't just messing about, and I was forced to kill them and bury
them under the patio so no other such code could ever escape. Don't make
me do the same thing to you!

Chris

Back to top
Gianni Mariani
Guest





PostPosted: Fri Feb 25, 2005 5:13 pm    Post subject: Re: const function? Reply with quote

Chris Jefferson wrote:
.... Of course I've only ever seen someone write this once when
Quote:
they weren't just messing about, and I was forced to kill them and bury
them under the patio so no other such code could ever escape. Don't make
me do the same thing to you!

I'm running... :)

How about this:

class Buffer
{
char * buf;

public:

Buffer( char * buf )
: buf( buf )
{
}

char * Get() { return buf; }
const char * Get() const { return buf; }
};

Sometimes (this is a simple case) you do want to do somthing different.

const containers usually want to do somthing very different with accessors.





Back to top
Shezan Baig
Guest





PostPosted: Sat Feb 26, 2005 3:20 am    Post subject: Re: const function? Reply with quote


Peter Koch Larsen wrote:
Quote:
"franco ziade" <fziade (AT) videotron (DOT) ca> skrev i en meddelelse
news:6qGTd.48193$6U2.948612 (AT) weber (DOT) videotron.net...
what does this declaration mean in C++

void func_name() const
{ ... }

is this a const function ?
what are the implications?

thanks


This does not compile. Probably from a textbook where "..." intends
to mean
that the body is left out.

/Peter



Thanks for pointing that out, Peter. :)


Back to top
Chris Jefferson
Guest





PostPosted: Sat Feb 26, 2005 5:07 pm    Post subject: Re: const function? Reply with quote

Gianni Mariani wrote:
Quote:
Chris Jefferson wrote:
... Of course I've only ever seen someone write this once when

they weren't just messing about, and I was forced to kill them and
bury them under the patio so no other such code could ever escape.
Don't make me do the same thing to you!


I'm running... :)

How about this:

class Buffer
{
char * buf;

public:

Buffer( char * buf )
: buf( buf )
{
}

char * Get() { return buf; }
const char * Get() const { return buf; }
};

Sometimes (this is a simple case) you do want to do somthing different.

const containers usually want to do somthing very different with accessors.


*slaps forehead*.. thats what I get for ever saying "Don't do X in C++"
for any X :)

Yes of course, I should have said (thanks!) that you can overload
functions differently to preserve const correctness, and in fact all the
standard contaners do that...

Chris

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.