Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const function?

Reply
Thread Tools

const function?

 
 
franco ziade
Guest
Posts: n/a
 
      02-25-2005
what does this declaration mean in C++

void func_name() const
{ ... }

is this a const function ?
what are the implications?

thanks


 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      02-25-2005
"franco ziade" <(E-Mail Removed)> writes:

>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/...+/argsC++.html
may help.
 
Reply With Quote
 
 
 
 
Peter Koch Larsen
Guest
Posts: n/a
 
      02-25-2005

"franco ziade" <(E-Mail Removed)> skrev i en meddelelse
news:6qGTd.48193$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      02-25-2005
franco ziade wrote:
> 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)

> 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;
}


>
> thanks
>
>

 
Reply With Quote
 
Chris Jefferson
Guest
Posts: n/a
 
      02-25-2005
franco ziade wrote:
> 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
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      02-25-2005
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.




 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      02-26-2005

Peter Koch Larsen wrote:
> "franco ziade" <(E-Mail Removed)> skrev i en meddelelse
> news:6qGTd.48193$(E-Mail Removed)...
> > 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.

 
Reply With Quote
 
Chris Jefferson
Guest
Posts: n/a
 
      02-26-2005
Gianni Mariani wrote:
> 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
 
Reply With Quote
 
Shailendra Kesarwani Shailendra Kesarwani is offline
Junior Member
Join Date: Jan 2011
Posts: 1
 
      01-18-2011
Quote:
Originally Posted by Gianni Mariani
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.
Can you please explain which buffer does the constant function return and why?
Also why does it return a const pointer?
I think the non-const function would return class variables value.
 
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
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments