Something I don't understand in the FAQ

A

Aguilar, James

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-lite/operator-overloading.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?
 
J

John Harrison

Aguilar said:
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-lite/operator-overloading.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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

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.
 
A

Aguilar, James

John Harrison said:

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.
 
J

John Harrison

Aguilar said:
John Harrison said:

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
 

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

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top