473,624 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const after function name

Hi all,

class Homer {
public:
int dot(int) const {return 1;}
...
}

what's the meaning of const after function name?

Best regards,
Robert

Aug 18 '05 #1
9 21347
"Robert" <zh*******@gmai l.com> writes:
what's the meaning of const after function name?

Try
http://www-h.eng.cam.ac.uk/help/tpl/...+/argsC++.html

Aug 18 '05 #2
Const at the end of function tells the compiler that your function wont
modify any private variables of the class.You can only use these
functions with const objects.

Gevadas A. Akkara

Aug 18 '05 #3
On 18 Aug 2005 01:29:33 -0700, "Robert" <zh*******@gmai l.com> wrote:
Hi all,

class Homer {
public:
int dot(int) const {return 1;}
...
}

what's the meaning of const after function name?

Best regards,
Robert


'dot' doesn't modify anything in 'Homer'. You'll get warned if you do
try to modify something. Not necessary, but may enable optimisation.

Dom
Aug 18 '05 #4
<ge*****@gmail. com> schrieb im Newsbeitrag
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Const at the end of function tells the compiler that your function wont
modify any private variables of the class.You can only use these
functions with const objects.

Gevadas A. Akkara


Not only private members.
const after function name makes the this-pointer const. So you are not
allowed to change a member variable in this function.

Greetings Chris
Aug 18 '05 #5
You won't "warned" you'll get a compiler error.
A const function is also not allowed to:
- Call a non-const member function
- Return by reference any member variable (other than by
const-reference).

A const function may return a member pointer (to non-const).

Aug 18 '05 #6
How about mutable member variables, can they be modified in const
member functions?

Aug 18 '05 #7
Frank Chang wrote:

How about mutable member variables, can they be modified in const
member functions?


Yes.

--
Karl Heinz Buchegger
kb******@gascad .at
Aug 18 '05 #8

Frank Chang wrote:
How about mutable member variables, can they be modified in const
member functions?


Yes they are the 'only' kind of data members that can be modified in a
const member function.

Aug 18 '05 #9
On Thu, 18 Aug 2005 10:51:34 +0200, "Christian Meier" <ch***@gmx.ch >
wrote in comp.lang.c++:
<ge*****@gmail. com> schrieb im Newsbeitrag
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Const at the end of function tells the compiler that your function wont
modify any private variables of the class.You can only use these
functions with const objects.

Gevadas A. Akkara


Not only private members.
const after function name makes the this-pointer const. So you are not
allowed to change a member variable in this function.


I hope you mean:

The const keyword after a member function name makes the object *this
constant inside the function.

'this' itself is always constant.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Aug 19 '05 #10

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

Similar topics

19
2787
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
4
1787
by: Dave | last post by:
Hello NG, In a thread I had started a long time ago, the conclusion had been reached that there is no good way to select between calling const vs non-const methods of the same name. (Of course, the object in question is non-const so that both may be legally called.) However, it appears I have indeed found a way to do it. I'm sure hoping nobody can poke any holes in my little scheme, but I want to throw it out there to see if it stands...
2
3625
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)
16
41757
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the declaration of a const pointer? Is the following program legal C? int main(int argc, char *argv) {
34
31280
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
4
2821
by: Kyku | last post by:
Hello all, I'm in a process of writing a small Linux C++ program for discovering repeaded files over a filesystem. One part of this task is traversing a directory structure. This is done by the following recursive function. Since I'd like it to be general enough to handle function pointers and function objects I've made it a template. template <typename CallBack> void Traverse(const std::string& path, CallBack& callback) throw
0
1930
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from. so here is the error I'm getting: TwoDMap.cpp: In member function 'void TwoDMap::setArc(const...
10
2177
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
23
2309
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
0
8177
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
8681
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
8629
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
8488
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
6112
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
4183
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2611
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
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.