473,320 Members | 1,841 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.

what does the const mean??

class X {
// members
public:
int foo() const;

}
int X::foo() const // what is the const for???
{

//do something
}

Jul 22 '05 #1
5 1377
gouki wrote:
class X {
// members
public:
int foo() const;

}
int X::foo() const // what is the const for???
{

//do something
}


It states that the X::foo() function does not modify the "observable"
state of the object. In other words from the callers perspective it
doesn't matter whether someone has called the X::foo() in past or not.
A 'getter' function is a typical example of a const function.

When a member function is declared const, that function cannot change
member variables or call other non-const functions on itself or its
members. An excepion to this rule are 'mutable' members, which may be
modified by a const function. Mutable members are typically used for
caching purposes.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #2
On Thu, 29 Jul 2004 10:15:18 +0200, Peter van Merkerk
<me*****@deadspam.com> wrote:
gouki wrote:
class X {
// members
public:
int foo() const;

}
int X::foo() const // what is the const for???
{

//do something
}


It states that the X::foo() function does not modify the "observable"
state of the object. In other words from the callers perspective it
doesn't matter whether someone has called the X::foo() in past or not.
A 'getter' function is a typical example of a const function.

When a member function is declared const, that function cannot change
member variables or call other non-const functions on itself or its
members.


And, importantly, const members can be called on const objects or
through const pointers and references. e.g.

const X& x = ...;
//x.nonconst(); //error
x.constmem(); //fine

Tom
Jul 22 '05 #3
gouki wrote:
class X {
// members
public:
int foo() const;

}
int X::foo() const // what is the const for???
{

//do something
}


I found this:
http://msdn.microsoft.com/library/de...m/tions_25.asp

regards marbac
Jul 22 '05 #4
On 29 Jul 2004 00:46:10 -0700 in comp.lang.c++, "gouki"
<fi*****@gmail.com> wrote,
int X::foo() const // what is the const for???


This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[18.10] What is a "const member function"?" It is always good to
check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Jul 22 '05 #5
Lot's of discussion about implications of the const qualifier on a
method declaration. What it means to the compiler is that the "this"
pointer inside foo is const X*, instead of X*. References to member
functions or data of class X (or it's parent classes) are implicitly
references to this->member.

dag
---
gouki wrote:
class X {
// members
public:
int foo() const;

}
int X::foo() const // what is the const for???
{

//do something
}


Jul 22 '05 #6

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

Similar topics

2
by: CoolPint | last post by:
Can anyone clearly explain the difference between constant reference to pointers and reference to constant pointers? What is const int * & ? Is it a constant reference to a pointer to an...
9
by: datastructure | last post by:
Copyright (c) 2003 by James J. Perry. All Rights Reserved. char Square::validList = {'r', 'g', 'b'}; //missing an element, is 0 int Square::numValues = 3; Square::Square() { value =...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
6
by: G Patel | last post by:
I've been looking at some code for string functions (certain implementation of them) and the non modified string is usually declared as a const char *s in the parameter list. I was wondering,...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
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 =...
7
by: rsk | last post by:
char *i_reg_fname = "none"; -- Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/ More information at http://www.talkaboutprogramming.com/faq.html
5
by: brettcclark | last post by:
This define is from the Vista SDK from propidl.h: #ifdef __cplusplus #define REFPROPVARIANT const PROPVARIANT & #else #define REFPROPVARIANT const PROPVARIANT * __MIDL_CONST #endif
2
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: 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...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.