473,976 Members | 7,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2982
"franco ziade" <fz****@videotr on.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****@videotr on.ca> skrev i en meddelelse
news:6q******** ************@we ber.videotron.n et...
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
2824
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 version of that type. Is this a bug that is specific to gcc, or is it a flaw in the language specification that gcc diligently implements? For example, the below program produces the output Constant Mutable
2
3656
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
7
3812
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. And why would one choose one over the other. moreover, I thought there was a difference between the two below where one would not let the value change whereas the other one would not let the
7
4383
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
8
2604
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. char * -> const char *. However, this does not appear to work when I add another level of indirection: void test1 (char **value) {}
8
10151
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) When I asked why, I was told that it facilitated calling it as:
20
2503
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 doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
16
3184
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 parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed inside the function. And it is for this reason that some people advocate that it is a good idea to...
4
6712
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 = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
6
2416
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
10352
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10170
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11407
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11570
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7602
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6554
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.