mutable member and const method help

Discussion in 'C++' started by wtnt, May 7, 2004.

  1. wtnt

    wtnt Guest

    Hello.

    I previously had a program that compiled and worked with no error.
    Relevant parts here:

    class BasicList{
    public:
    char* listLookup() {
    item = buffer;
    ...
    return something;
    }
    protected:
    char* item;
    char buffer[128];
    ...
    }

    Now I try to make it const correct so I changed it to be this with the
    const and mutable keywords:

    class BasicList{
    public:
    char* listLookup() const{
    item = buffer;
    ...
    return something;
    }
    protected:
    mutable char* item;
    char buffer[128];
    ...
    }

    Now when I compile, I get an error at the assignment line
    (item=buffer), saying invalid conversion from 'const char*' to
    'char*'. I don't really understand why this error occurs, when it
    didn't before the method was const. Why? and how to fix?

    Your help is appreciated.
    thank you!
    wtnt, May 7, 2004
    #1
    1. Advertising

  2. wtnt wrote:
    > ...
    > Now I try to make it const correct so I changed it to be this with the
    > const and mutable keywords:
    >
    > class BasicList{
    > public:
    > char* listLookup() const{
    > item = buffer;
    > ...
    > return something;
    > }
    > protected:
    > mutable char* item;
    > char buffer[128];
    > ...
    > }
    >
    > Now when I compile, I get an error at the assignment line
    > (item=buffer), saying invalid conversion from 'const char*' to
    > 'char*'. I don't really understand why this error occurs, when it
    > didn't before the method was const. Why? and how to fix?
    > ...


    Inside a 'const' method data member 'buffer' has type 'const char[128]'.
    You are trying to convert it to type 'char*'. It is impossible. 'const
    char[128]' can be converted to 'const char*', but not to 'char*'. That's
    what the compiler is trying to tell you.

    The immediate solution would be to declare 'buffer' mutable as well.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, May 7, 2004
    #2
    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. pit3k
    Replies:
    2
    Views:
    1,002
    sadhu
    Feb 8, 2005
  2. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  3. Replies:
    17
    Views:
    1,627
    James Kanze
    Oct 5, 2007
  4. Replies:
    6
    Views:
    456
    Andrey Tarasevich
    Feb 22, 2008
  5. fungus
    Replies:
    13
    Views:
    882
    fungus
    Oct 31, 2008
Loading...

Share This Page