const function problem. casting to mutable?

P

pit3k

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?
 
S

sadhu

Use a const_cast.
bool is_valid() const {
return ! (const_cast<otl_stream>(m_otl_stream)).eof();
}
 
S

sadhu

Use a const_cast.
bool is_valid() const {
return ! (const_cast<otl_stream>(m_otl_stream)).eof();
}
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top