473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

const qualifier problem


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; }
};

Jul 22 '05 #1
5 1027

"Patrick Guio" <pa******@ii.uib.no> wrote in message
news:Pi*******************************@eukalyptus. ii.uib.no...

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
Jul 22 '05 #2
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);

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

Jul 22 '05 #3

"Patrick Guio" <pa******@ii.uib.no> wrote in message
news:Pi*******************************@eukalyptus. ii.uib.no...
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
Jul 22 '05 #4
Jeff Flinn wrote:
"Patrick Guio" <pa******@ii.uib.no> wrote in message
news:Pi*******************************@eukalyptus. ii.uib.no...
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);

--
Regards,
Slava

Jul 22 '05 #5
Vyacheslav Kononenko wrote:
Jeff Flinn wrote:
"Patrick Guio" <pa******@ii.uib.no> wrote in message
news:Pi*******************************@eukalyptus. ii.uib.no...
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...

--
Regards,
Slava

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
4
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is...
13
by: matthias_k | last post by:
Hi, I've never thought about this before, but since you are able to overload a function only by changing the const-ness of the formal parameters, some annoying side effects will arise. For...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
9
by: sarathy | last post by:
Hi, Can anyone just help me with what exactly is constant expression. I read the section on Constant expression in K&R2. i am not fully clear with it.
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
2
by: subramanian100in | last post by:
Consider the following program: #include <stdio.h> void myfn(const int **a) { static int i, j, k; a = &i; a = &j;
5
by: amvoiepd | last post by:
Hi, My question is about how to use const properly. I have two examples describing my problem. First, let's say I have a linked list and from it I want to find some special node. I write the...
4
by: Ben Petering | last post by:
Hi group, this is a 'best practice' type question (I want discussion of the issue - whys and why nots - similar to "casting the return value of malloc()", to cite an analogous case). Let's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.