473,394 Members | 1,946 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,394 software developers and data experts.

Short question

Just a little quick question what is the effect of putting a const after
a member function definition.

Charles

Jul 19 '05 #1
3 3325
"Charles Keepax" <s9******@sms.ed.ac.uk> wrote...
Just a little quick question what is the effect of putting a const after
a member function definition.


It makes the function 'const'. Inside it 'this' designates
a constant object. Functions can be overloaded based on that.
For a non-const object non-const function is preferred, for
a const object only a const function will be called.

struct A {
A() {}
void foo() const;
void foo();
};

int main() {
A a;
a.foo(); // non-const is called
const A ca;
ca.foo(); // const is called
}

Doesn't the book on C++ that you're using for study explain
all that?

Victor
Jul 19 '05 #2

"Charles Keepax" <s9******@sms.ed.ac.uk> wrote in message
news:bf**********@scotsman.ed.ac.uk...
Just a little quick question what is the effect of putting a const after
a member function definition.

Charles


Its a guarantee that the member function will not alter the object it is
called on. It means that the member function can be called on a const object
or via a const reference or const pointer.

John
Jul 19 '05 #3

"Charles Keepax" <s9******@sms.ed.ac.uk> wrote in message
news:bf**********@scotsman.ed.ac.uk...
Just a little quick question what is the effect of putting a const after
a member function definition.

Little quick answer :-)
The function does not modify the state of the object/class..
There are two camps on what that implies -
bitwise constness or abstract state constness.
Bitwise constness implies that no member variable gets modified.
For abstract constness consider a class
class A{
public :
void copy () ; // changes what p points to, not p..hence not const

private:
char *p;
};

Even if there is bitwise constness ( p unchanged) but if the contents of
what p points to get changed then
it is not abstract state constness.

--
With best wishes
J.Schafer
Jul 19 '05 #4

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

Similar topics

10
by: Niels Dekker (no reply address) | last post by:
Is it possible for a standard compliant C++ compiler to have ( sizeof(short) < sizeof(int) ) and ( sizeof(short) == sizeof((short)0 + (short)0) ) ? Regards, Niels Dekker...
3
by: PhoneJack | last post by:
I am writting an algorithm that generates very large tables in dynamic memory on a linux system. These 2 tables are of the order of (30,000 x 30,000). They are being generated by the following...
99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
18
by: a | last post by:
short s; long l; s= -2; l= -3; printf("% _ %_",s, l); What characters should be filled out in the formatted string for output? Thanx
29
by: john | last post by:
I read in the first question in the FAQ that both short and int are guaranteed to be able to hold values up to 32,767. Then why would one use an int instead of a short if short takes less space?
8
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes ....
7
by: Partho | last post by:
I have a float variable which I need to add to a short variable. How do I do this? Do I have to typecast or is there a way around? I tried typecasting the float to a short, but that gives me a 0 or...
0
by: wolverine | last post by:
Hi I want to know how to use basic_string with unsigned short (I have mentioned below why i have to do this). Could any tell me some good references in this topic. I am new to creating a new...
7
by: Z-Z | last post by:
Hi, I used the following macro to determine variable signed or not, but only find that it is useless for a short or char variable. I think that it might caused by value conversion rules. I used...
3
by: TS | last post by:
short x = 1; //this has build error saying missing conversion x = x + 2; Why can't you just add this? I guess the 1 is a literal that translates to an int32? How would I accomplish this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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,...
0
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...
0
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...

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.