const qualifier problem

P

Patrick Guio

Dear all,

I cannot figure out why I cannot have the operator() member function
declared const?
This operation shouldn't modify anything in my class...

Sincerely,
Patrick


#include <map>
class functions
{
public:
functions() {
funs[1] = &functions::f1;
}
// cannot compile with operator() declared const.
// double operator()(double x) const { return (this->*funs[1])(x); }
double operator()(double x) { return (this->*funs[1])(x); }
protected:
typedef std::map<int, double (functions::*)(double) const> FunctionsMap;
FunctionsMap funs;
double f1(double x) const { return x*x; }
};
 
J

John Harrison

Patrick Guio said:
Dear all,

I cannot figure out why I cannot have the operator() member function
declared const?
This operation shouldn't modify anything in my class...

But the operator[] on a map does (potentially) modify the map. Use the find
method instead.

john
 
P

Patrick Guio

I cannot figure out why I cannot have the operator() member function
declared const?
This operation shouldn't modify anything in my class...

But the operator[] on a map does (potentially) modify the map. Use the find
method instead.

Ok I understand your point. I can use an intermediate FunctionsMap::iterator
like
FunctionsMap::iterator i = this->funs.find(1);

But I cannot figure out how to evaluate the function itself? I tried
i->second(x);
but it does not compile.
Any idea?
Sincerely,
Patrick
 
J

Jeff Flinn

Patrick Guio said:
I cannot figure out why I cannot have the operator() member function
declared const?
This operation shouldn't modify anything in my class...

But the operator[] on a map does (potentially) modify the map. Use the find
method instead.

Ok I understand your point. I can use an intermediate FunctionsMap::iterator
like
FunctionsMap::iterator i = this->funs.find(1);
FunctionsMap::const_iterator

But I cannot figure out how to evaluate the function itself? I tried
i->second(x);
but it does not compile.

use the above const_iterator and it should.

Jeff F
 
V

Vyacheslav Kononenko

Jeff said:
I cannot figure out why I cannot have the operator() member function
declared const?
This operation shouldn't modify anything in my class...


But the operator[] on a map does (potentially) modify the map. Use the
find
method instead.

Ok I understand your point. I can use an intermediate
FunctionsMap::iterator

like
FunctionsMap::iterator i = this->funs.find(1);

FunctionsMap::const_iterator


But I cannot figure out how to evaluate the function itself? I tried
i->second(x);
but it does not compile.


use the above const_iterator and it should.

Jeff F
No it will not. It should be:
(this->*(i->second))(x);
 
V

Vyacheslav Kononenko

Vyacheslav said:
Jeff said:
On Tue, 9 Nov 2004, John Harrison wrote:


I cannot figure out why I cannot have the operator() member function
declared const?
This operation shouldn't modify anything in my class...


But the operator[] on a map does (potentially) modify the map. Use the

find

method instead.


Ok I understand your point. I can use an intermediate

FunctionsMap::iterator

like
FunctionsMap::iterator i = this->funs.find(1);


FunctionsMap::const_iterator


But I cannot figure out how to evaluate the function itself? I tried
i->second(x);
but it does not compile.



use the above const_iterator and it should.

Jeff F
No it will not. It should be:
(this->*(i->second))(x);
Actually just (this->*i->second)(x); works as well but it is too ugly
for me...
 

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,183
Messages
2,570,967
Members
47,520
Latest member
KrisMacono

Latest Threads

Top