const function?

F

franco ziade

what does this declaration mean in C++

void func_name() const
{ ... }

is this a const function ?
what are the implications?

thanks
 
P

Peter Koch Larsen

franco ziade said:
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
 
G

Gianni Mariani

franco said:
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;
}
 
C

Chris Jefferson

franco said:
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
 
G

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

Shezan Baig

Peter said:
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. :)
 
C

Chris Jefferson

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



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
 
Joined
Jan 18, 2011
Messages
1
Reaction score
0
Gianni Mariani said:
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.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top