473,499 Members | 1,974 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 21332
"Robert" <zh*******@gmail.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*******@gmail.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.googlegr oups.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.googlegr oups.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.learn.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
2758
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...
4
1781
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,...
2
3614
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...
16
41741
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,...
34
31222
by: Perro Flaco | last post by:
Hi! I've got this: string str1; char * str2; .... str1 = "whatever"; .... str2 = (char *)str1.c_str();
4
2801
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...
0
1912
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*....
10
2152
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
2296
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
7007
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...
0
7220
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...
1
6894
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...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
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...
0
4600
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...
0
3099
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...
0
1427
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 ...
1
665
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.