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

Something I don't understand in the FAQ

P: n/a
I thought that functions always had to have different parameters if they had
the same names. However, this section
(http://www.parashift.com/c++-faq-lit....html#faq-13.8)
of the FAQ creates two operator () methods that take the same parameters.
One is const, the other is not, and the return type is different. However,
I was under the impression that methods with identical names and parameter
signatures would be an error. How does this work?
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote in message
news:cf**********@newsreader.wustl.edu...
I thought that functions always had to have different parameters if they had the same names. However, this section
(http://www.parashift.com/c++-faq-lit....html#faq-13.8)
of the FAQ creates two operator () methods that take the same parameters.
One is const, the other is not, and the return type is different. However, I was under the impression that methods with identical names and parameter
signatures would be an error. How does this work?


Because whether a method is const or not is part of its signature.

This is a common and useful technique. In the FAQ is has been used to create
two different operator() methods, only one of which (the non-const version)
allows you to modify the Matrix.

john
Jul 22 '05 #2

P: n/a
Aguilar, James wrote:
One is const, the other is not, and the return type is different.
However, I was under the impression that methods with identical names and
parameter
signatures would be an error. How does this work?


The constness of a member function is part of his signature.

You can see it that way: a non static member function has this as a hidden
parameter, if the function is const, is a pointer to a const object, if
not, a pointer to a non-const onbject. Then the parameter list is not
exzactly identical.

--
Salu2
Jul 22 '05 #3

P: n/a
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2n************@uni-berlin.de...

[snip]


I'm sorry, maybe I should have formulated my question better. Here's my
real question. How does it know which to pick in these two cases:

m(5,8) = 106.15;
std::cout << m(5,8);

? The function in the first line still doesn't modify the object. The
object ends up modified because of the call, but how does the compiler know
about that when it's deciding what method to call? I thought the const
suffix only applied to things that happen inside the method.
Jul 22 '05 #4

P: n/a

"Aguilar, James" <jf**@cec.NOBOTSwustl.edu> wrote in message
news:cf**********@newsreader.wustl.edu...
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2n************@uni-berlin.de...

[snip]
I'm sorry, maybe I should have formulated my question better. Here's my
real question. How does it know which to pick in these two cases:

m(5,8) = 106.15;
std::cout << m(5,8);

? The function in the first line still doesn't modify the object. The
object ends up modified because of the call, but how does the compiler

know about that when it's deciding what method to call? I thought the const
suffix only applied to things that happen inside the method.


You are right, whether operator() is being use to read or write an element
of the Matrix is irrelevant.
What determines which version of operator() is called depends on how m is
declared. If m is a const object or a const reference then the const version
gets called, otherwise the non-const version gets called.

john
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.