Use const_cast to modify member variable?

Discussion in 'C++' started by Immortal Nephi, Jul 1, 2008.

  1. How can I modify member variable inside class if member function has
    const like mem_Func(void) const. Please do not offer the keyword --
    mutable. I want to know if keyword -- const_cast can be done.

    If mutable is only the solution, I will follow not to use const_cast.
    Immortal Nephi, Jul 1, 2008
    #1
    1. Advertising

  2. Immortal Nephi

    Kai-Uwe Bux Guest

    Immortal Nephi wrote:

    > How can I modify member variable inside class if member function has
    > const like mem_Func(void) const. Please do not offer the keyword --
    > mutable.


    Why not? It's meant to be used for member variables that do not affect
    logical constness. If the member variable you are modifying better not be
    declared mutable, you probably shouldn't be modifying it in a const member
    function.

    > I want to know if keyword -- const_cast can be done.


    Yes it can be used to cast away constness, but it will produce undefined
    behavior if you use it on an object declared const.

    > If mutable is only the solution, I will follow not to use const_cast.


    What is the underlying problem that you are trying to solve? I looks as
    though you might be headed the wrong direction, and in order to give better
    advice some additional information about the background would be good.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jul 1, 2008
    #2
    1. Advertising

  3. Immortal Nephi

    Eric Pruneau Guest

    "Immortal Nephi" <> a écrit dans le message de
    news: ...
    > How can I modify member variable inside class if member function has
    > const like mem_Func(void) const. Please do not offer the keyword --
    > mutable. I want to know if keyword -- const_cast can be done.
    >
    > If mutable is only the solution, I will follow not to use const_cast.


    Ok it is Evil but you can do it.

    struct EvilClass
    {
    void EvilFunct() const;
    int _a;
    };

    void EvilClass::EvilFunct() const
    {
    int& tmp = const_cast<int&>(_a);
    tmp = 5; // now _a == 5
    }


    You can also define a template function if you have many variables like
    that.

    struct EvilClass
    {
    void EvilFunct() const;

    template<typename T>
    void EvilSet(T const& val, T const& NewVal) const;

    int _a;
    };

    void EvilClass::EvilFunct() const
    {
    EvilSet(_a, 5); // much cleaner but maybe also much evil
    }

    template<typename T>
    void EvilClass::EvilSet(T const& val, T const& NewVal) const
    {
    T& tmp = const_cast<T&>(val);
    tmp = NewVal;
    }

    ------------------------

    Eric Pruneau
    Eric Pruneau, Jul 1, 2008
    #3
    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. Kaspar Minosiants

    [help] const_cast

    Kaspar Minosiants, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    411
    John Harrison
    Jul 21, 2003
  2. CoolPint
    Replies:
    20
    Views:
    1,761
    Old Wolf
    Jul 26, 2004
  3. Qi
    Replies:
    11
    Views:
    1,144
    Narinder
    Jul 12, 2011
  4. kath
    Replies:
    0
    Views:
    176
  5. Replies:
    2
    Views:
    215
Loading...

Share This Page