Modifying non-const object from const function?

F

fdm

I have a const function where it should be possible to modify an object but
not the argument:

void myfun(const int point) const {

m_Container.push_back(point);


}

But I get the error:

Error 13 error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this'
pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'

The above is a simplified example of a library that I am supposed to modify
without changing the function declarations. How do I make it possible to
modify the std::vector m_Container from the above const function?
 
F

Francesco

I have a const function where it should be possible to modify an object but
not the argument:

void myfun(const int point) const {

m_Container.push_back(point);

}

But I get the error:

Error 13 error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this'
pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'

The above is a simplified example of a library that I am supposed to modify
without changing the function declarations. How do I make it possible to
modify the std::vector m_Container from the above const function?

Hi,
I suppose the solution to your problem could be to declare m_Container
as mutable, but to get better help you should post sufficient code for
us to analyze.

Please have a look to the clc++ FAQ http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Cheers,
Francesco
 
F

fdm

hm I can only see the replies when using google and not when I use Outlook
Express. Maybe its my news server that is slow.
 
J

Jerry Coffin

I have a const function where it should be possible to modify an object but
not the argument:

void myfun(const int point) const {

m_Container.push_back(point);


}

But I get the error:

Error 13 error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this'
pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &'

The above is a simplified example of a library that I am supposed to modify
without changing the function declarations. How do I make it possible to
modify the std::vector m_Container from the above const function?

If you're modifying a member variable, then either the function
shouldn't be declared const:

void myfun(const int point) {

m_Container.push_back(point);
}

Or m_Container should be defined to be mutable. As a stop-gap, you
can cast away the constness, though this is generally a poor idea.
 
F

Francesco

If you're modifying a member variable, then either the function
shouldn't be declared const:

void myfun(const int point) {

        m_Container.push_back(point);

}

Or m_Container should be defined to be mutable. As a stop-gap, you
can cast away the constness, though this is generally a poor idea.

Jerry, just a question to clear a doubt of mine: did you receive
Pascal's and my replies to this thread? I'm noticing that sometimes
people posts replies which contain already pointed out things, and
that's strange. Maybe Google Groups is not sending out replies to the
other servers immediately as expected, hence the delay noticed by fdm
and your reply... I don't know.

Just out of curiosity,
king regards,
Francesco
 
F

Francesco

hm I can only see the replies when using google and not when I use Outlook
Express. Maybe its my news server that is slow.

Fine, now you read all of our replies. Have you solved this issue?

And what about the other thread of yours that's still open - the one
titled "Possible problem with smartpointers (for the hardcore C++
gurus)"?

Best regards,
Francesco
 
F

fdm

hm I can only see the replies when using google and not when I use Outlook
Express. Maybe its my news server that is slow.

Fine, now you read all of our replies. Have you solved this issue?

Yes, was solved declaring the vector as mutable.

And what about the other thread of yours that's still open - the one
titled "Possible problem with smartpointers (for the hardcore C++
gurus)"?


yes is was just a matter of moving the parameters out of the subroutine.
 
J

Jerry Coffin

(e-mail address removed)>, (e-mail address removed)
says...

[ ... ]
Jerry, just a question to clear a doubt of mine: did you receive
Pascal's and my replies to this thread? I'm noticing that sometimes
people posts replies which contain already pointed out things, and
that's strange. Maybe Google Groups is not sending out replies to the
other servers immediately as expected, hence the delay noticed by fdm
and your reply... I don't know.

Welcome to NNTP! I received 38 new messages at once, including one
from Pascal and two from you (one directly to the OP, and one to
Pascal) at exactly the same time I received the post to which I'm
following up.

A server doesn't normally send out replies immediately. Rather, it
has some other servers it connects to on some sort of schedule, and
when it connects it sends any new posts it has, and collects any new
posts the other has. Ultimately, it's just about like when a
newsreader connects to a server, sending any new posts it has (i.e.
whatever I've written) and collecting any new posts on that server.

If we connected to the same server, the delay between one of us
posting an article and somebody else seeing that article would
normally be expected to be pretty short -- typically in the range of
a few milliseconds up to a few seconds or so. When we're connected to
separate servers, however, the delay can be substantially longer.
Looking at the Path header, I see that your post traveled through
about 15 servers to get from you to me, so it's not a great surprise
that it takes a while.

The other thing to keep in mind is that there's no real central
management of any of this -- it's pretty much up to individual site
operators to decide what other server(s) they connect and exchange
news with. The protocol is fairly robust in tracking messages so you
don't see duplicates if a single post arrives by different routes
(barring a mis-configured server munging things up). There's far
less, however, to guarantee that a post will get to every server --
for that, it's pretty much up to individual sites to connect to
enough other servers to provide redundancy so when any given path
dies, there will probably be at least one other that works.
 
J

Juha Nieminen

fdm said:
Yes, was solved declaring the vector as mutable.

I hope you realize that the 'mutable' keyword should not be used
lightly, from a program design point of view.

When you declare a member function 'const', you are making the promise
that the function in question will not change the state of the object.
In other words, the object will look to the outside completely identical
after the function call as it looked prior to it. In other words, the
function does not change the object in any way (as viewed from the outside).

If you bypass the constness of a member function and change the state
of the object (in a way that is visible to the outside), you are
breaking this promise, and potentially breaking code which uses it. At
the very least it's very dubious design.

Of course this doesn't mean there are no situations where 'mutable'
would be useful. From a design point of view using it is ok only if it
changes an internal state which is not visible to the outside in any
way. In other words, even after the change the object will still behave
identically, and it's not possible to tell from the outside that it has
changed.

One concrete situation where 'mutable' is both very practical and
completely ok to use (from a design point of view) is a smart pointer
which uses double-linking instead of reference counting. (The copy
constructor and the assignment operator take const references, but the
pointers in the parameter have to be changed. However, this does not
change the external behavior of the smart pointer object.)
 
F

Francesco

(e-mail address removed)>, (e-mail address removed)
says...

[ ... ]
Jerry, just a question to clear a doubt of mine: did you receive
Pascal's and my replies to this thread? I'm noticing that sometimes
people posts replies which contain already pointed out things, and
that's strange. Maybe Google Groups is not sending out replies to the
other servers immediately as expected, hence the delay noticed by fdm
and your reply... I don't know.

Welcome to NNTP! I received 38 new messages at once, including one
from Pascal and two from you (one directly to the OP, and one to
Pascal) at exactly the same time I received the post to which I'm
following up.

[snip]

Crystal clear: I had no idea about all the issues you detailed.
Thanks a lot for your explanation.

Best regards,
Francesco
 
F

Francesco

[snip]
Fine, now you read all of our replies. Have you solved this issue?

Yes, was solved declaring the vector as mutable.
And what about the other thread of yours that's still open - the one
titled "Possible problem with smartpointers (for the hardcore C++
gurus)"?

yes is was just a matter of moving the parameters out of the subroutine.

Fine, glad to read that you solved all of those issues.

Sorry for hassling you, but it would be good to drop a message at the
end of the threads, once the issue is solved. Not simply for the
people who posted their advices there, but especially for other people
who could arrive there looking for a solution to a similar problem -
the "closing" post of the OP, resuming the problem and detailing the
solution, changes an open-issue thread to a useful resource,
eventually avoiding them to open a new topic or rehearse an old one.

Best regards,
Francesco
 

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
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top