Modifying non-const object from const function?

Discussion in 'C++' started by fdm, Aug 31, 2009.

  1. fdm

    fdm Guest

    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?
    fdm, Aug 31, 2009
    #1
    1. Advertising

  2. fdm

    Francesco Guest

    On 31 Ago, 12:32, "fdm" <> wrote:
    > 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
    Francesco, Aug 31, 2009
    #2
    1. Advertising

  3. Francesco <> writes:

    > On 31 Ago, 12:32, "fdm" <> wrote:
    >> 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


    However, in this case, manifestly the purpose of that function is to
    modify this object, so it should not be const.

    --
    __Pascal Bourguignon__
    Pascal J. Bourguignon, Aug 31, 2009
    #3
  4. fdm

    fdm Guest

    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.

    "fdm" <> wrote in message
    news:4a9ba6cc$0$279$...
    >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?
    >
    >
    fdm, Aug 31, 2009
    #4
  5. fdm

    Jerry Coffin Guest

    In article <4a9ba6cc$0$279$>,
    says...
    >
    > 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.

    --
    Later,
    Jerry.
    Jerry Coffin, Aug 31, 2009
    #5
  6. fdm

    Francesco Guest

    On 31 Ago, 15:26, Jerry Coffin <> wrote:
    > In article <4a9ba6cc$0$279$>,
    > says...
    >
    >
    >
    >
    >
    > > 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.


    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
    Francesco, Aug 31, 2009
    #6
  7. fdm

    Francesco Guest

    On 31 Ago, 15:16, "fdm" <> wrote:
    > 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.
    >
    > "fdm" <> wrote in message
    >
    > news:4a9ba6cc$0$279$...
    >
    > >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?

    >
    >


    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
    Francesco, Aug 31, 2009
    #7
  8. fdm

    fdm Guest

    "Francesco" <> wrote in message
    news:...
    On 31 Ago, 15:16, "fdm" <> wrote:
    > 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.
    >
    > "fdm" <> wrote in message
    >
    > news:4a9ba6cc$0$279$...
    >
    > >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?

    >
    >


    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.
    fdm, Sep 1, 2009
    #8
  9. fdm

    Jerry Coffin Guest

    In article <054bd69c-9a71-48d1-8dd0-
    >,
    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.

    --
    Later,
    Jerry.
    Jerry Coffin, Sep 1, 2009
    #9
  10. fdm wrote:
    > 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.)
    Juha Nieminen, Sep 1, 2009
    #10
  11. fdm

    Francesco Guest

    On 1 Set, 16:32, Jerry Coffin <> wrote:
    > In article <054bd69c-9a71-48d1-8dd0-
    > >,
    > 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
    Francesco, Sep 1, 2009
    #11
  12. fdm

    Francesco Guest

    On 1 Set, 02:02, "fdm" <> wrote:
    > "Francesco" <> wrote in message


    [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
    Francesco, Sep 1, 2009
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fabian Wein
    Replies:
    8
    Views:
    448
    terminator
    Jul 26, 2007
  2. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  3. fungus
    Replies:
    13
    Views:
    886
    fungus
    Oct 31, 2008
  4. Francesco
    Replies:
    0
    Views:
    403
    Francesco
    Aug 31, 2009
  5. none
    Replies:
    3
    Views:
    3,256
    James Kanze
    May 20, 2010
Loading...

Share This Page