473,320 Members | 1,828 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,320 software developers and data experts.

const function?

what does this declaration mean in C++

void func_name() const
{ ... }

is this a const function ?
what are the implications?

thanks
Jul 23 '05 #1
6 2957
"franco ziade" <fz****@videotron.ca> 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.
Jul 23 '05 #2

"franco ziade" <fz****@videotron.ca> skrev i en meddelelse
news:6q********************@weber.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
Jul 23 '05 #3
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

Jul 23 '05 #4
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
Jul 23 '05 #5
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.


Jul 23 '05 #6
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
Jul 23 '05 #7

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

Similar topics

19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
7
by: johny smith | last post by:
Can someone please explain to me the difference between these two: function1( const int a) function2( int const a) Both seemed to compile, but what is the difference between the two above....
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
16
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the...
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 =...
6
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam § Function() § { /...do something.../ someType var § = something; /...do something.../ return something;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.