const function problem. casting to mutable?

Discussion in 'C++' started by pit3k, Feb 8, 2005.

  1. pit3k

    pit3k Guest

    How can I call non-const member function of a member of a class in one of
    it's const qualified member function so that a compiler won't complain?

    Here's the code:

    bool otl_stream::eof();

    class MyClass {
    otl_stream m_otl_stream;
    public:
    bool is_valid() const {
    return !m_otl_stream.eof();
    }
    };


    The problem is the otl_stream::eof() function is not const qualified,
    although it does not change the state of the object (the prototype of this
    function is beyond my control).
    I want to call it in my MyClass::is_valid() method which is const qualified.
    How do I do this, without declaring m_otl_stream member as mutable?

    --
    pit3k
    pit3k, Feb 8, 2005
    #1
    1. Advertising

  2. pit3k

    sadhu Guest

    Use a const_cast.
    bool is_valid() const {
    return ! (const_cast<otl_stream>(m_otl_stream)).eof();
    }
    sadhu, Feb 8, 2005
    #2
    1. Advertising

  3. pit3k

    sadhu Guest

    Use a const_cast.
    bool is_valid() const {
    return ! (const_cast<otl_stream>(m_otl_stream)).eof();
    }
    sadhu, Feb 8, 2005
    #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. wtnt
    Replies:
    1
    Views:
    671
    Andrey Tarasevich
    May 7, 2004
  2. Replies:
    11
    Views:
    1,101
  3. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  4. Replies:
    17
    Views:
    1,631
    James Kanze
    Oct 5, 2007
  5. Alex Vinokur
    Replies:
    9
    Views:
    791
    James Kanze
    Oct 13, 2008
Loading...

Share This Page