473,763 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1393
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.merke rk(at)dse.nl
Jul 22 '05 #2
On Thu, 29 Jul 2004 10:15:18 +0200, Peter van Merkerk
<me*****@deadsp am.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
16242
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 integer? Or Is it a reference to a pointer to a constant integer? What is being constant in this case? The pointer or the integer being pointed? How about int * const & ? Is this a reference to a constant pointer to an integer? Or
9
407
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 = validList; } Square::Square(char val)
72
5890
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 example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
6
27772
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, what exactly does that mean. Does that mean the function is restricted from changing the address (changing s) or changing what's being pointed to (*s)?
15
2141
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 question when I was thinking about implementation of a class implementing some lazy data structure or cache. The C++ rules allow among other possibilities making all member functions non-const, or making all member functions const and all members...
4
6695
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
7
318
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
6759
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
13069
by: Lambda | last post by:
The code is simple: // Token.h #ifndef TOKEN_H #define TOKEN_H #include <vector> #include <string> class Token
18
2457
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 that one can implement a very basic form of subclassing in C (the gurus certainly already know this, but it was news to me). What i've done (shown below) seems to work all fine and well, and does exactly what i'd expect, but i'm asking about
0
9566
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10003
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...
0
9828
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...
1
7370
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
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
5271
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3918
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
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.