const function

T

thomas

compile error :

bool cmp(const int &x, const int &y) const{
return x>y;
}


it says that the third const should not be used. why?
 
B

Balog Pal

compile error :
bool cmp(const int &x, const int &y) const{
return x>y;
}

it says that the third const should not be used. why?

The third const only applies to member functions, not to freestanding ones.
 
R

Rolf Magnus

thomas said:
compile error :

bool cmp(const int &x, const int &y) const{
return x>y;
}


it says that the third const should not be used. why?

Because it makes no sense at that point. What do you expect to be the effect
of that 'const'?
 
T

Thomas Matthews

P.K.D said:
W dniu 2009-07-19 15:18, thomas pisze:

I think it can be only used inside a class not in any function.

Only methods inside a class, struct or union can be
declared as const.

The "const" specifier tells the compiler that no member
variables will be modified by this method.

Since functions declared outside the scope of a class do
not have any member variables, it can't modify them.

Also, this case applies to methods declared as static
within a class.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top