By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

why two const in one function

P: n/a
in a *.h file, there is a declaration:

const ClassName *functionName() const {return oneVar;}
I was confused by the two const there.
can anybody help explain why we need the first const.
why we need the second const here ?

Thanks
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jianli Shen wrote:
in a *.h file, there is a declaration:

const ClassName *functionName() const {return oneVar;}


Look at each part separately -- the "stuff" before the name of the
function is (mostly) the return type, while what comes after the name
refers to the function itself.

const ClassName *

That much is talking about the function's return value -- it returns a
pointer to a const ClassName.

function() const

That means the function itself is const -- i.e. that it's a member of a
class, and when it's invoked, 'this' points to a const X (X=whatever
its class type is).

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #2

P: n/a

"Jianli Shen" <ji****@cc.gatech.edu> wrote in message
news:d1**********@news-int2.gatech.edu...
in a *.h file, there is a declaration:

const ClassName *functionName() const {return oneVar;}
I was confused by the two const there.
can anybody help explain why we need the first const.
why we need the second const here ?

The case of the first const:
Here, functionName is a function that returns a pointer to a const
ClassName. See:
http://www.parashift.com/c++-faq-lit....html#faq-18.4 and
http://www.parashift.com/c++-faq-lit....html#faq-18.5

The reason for the second const is also covered by the FAQ:
http://www.parashift.com/c++-faq-lit...html#faq-18.10

Regards,
Sumit.
--
Sumit Rajan <su*********@gmail.com>
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.