473,405 Members | 2,444 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,405 software developers and data experts.

quick question about C++ function

bool foo (T const & x,T const & y) const{
...
}

the third const means what?

thanks in advance!

Jun 26 '06 #1
3 1101
Pingke posted:
bool foo (T const & x,T const & y) const{
...
}

the third const means what?

thanks in advance!

This is a member function, and it belongs within a class.

By placing "const" after a member function's name, you indicate that the
function can be invoked on a const object of the class. By default, no
member function can be invoked on a const object of a class, so you have
to explicitly enable this functionality by writing "const" after the
function signature.

class ArbitraryClass {
public:

void ConstFunc() const {}

void NonConstFunc() {}

};
int main()
{
ArbitraryClass obj1;

obj1.ConstFunc(); /* No problem */
obj1.NonConstFunc(); /* No problem */
ArbitraryClass const obj2;

obj2.ConstFunc(); /* No problem */
obj2.NonConstFunc(); /* COMPILE ERROR */
}

--

Frederick Gotham
Jun 26 '06 #2

"Pingke" <xl*****@gmail.com> wrote in message
news:11*********************@y41g2000cwy.googlegro ups.com...
bool foo (T const & x,T const & y) const{
...
}

the third const means what?

thanks in advance!

the method does not/cannot change the object this method is being applied to


Jun 26 '06 #3

Frederick Gotham wrote:
Pingke posted:
bool foo (T const & x,T const & y) const{
...
}

the third const means what?

thanks in advance!

This is a member function, and it belongs within a class.

By placing "const" after a member function's name, you indicate that the
function can be invoked on a const object of the class. By default, no
member function can be invoked on a const object of a class, so you have
to explicitly enable this functionality by writing "const" after the
function signature.

class ArbitraryClass {
public:

void ConstFunc() const {}

void NonConstFunc() {}

};
int main()
{
ArbitraryClass obj1;

obj1.ConstFunc(); /* No problem */
obj1.NonConstFunc(); /* No problem */
ArbitraryClass const obj2;

obj2.ConstFunc(); /* No problem */
obj2.NonConstFunc(); /* COMPILE ERROR */
}

--

Frederick Gotham


thank you very much, very clear

Jun 26 '06 #4

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

Similar topics

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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...
0
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...

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.