Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to change content of af CString in a const method

Status
Not open for further replies.

CruelIO

Programmer
Apr 15, 2005
5
DK
Hi i have the following method on my objects obj

BOOL obj::CloseRecord() const
{
//Do something

//Empty the member variable m_Id (is a CString)
static_cast<CString>(m_Id) = ""; //<--Doesnt work

//Empty set the member variable m_Item = 0
static_cast<int>(m_Item) = 0; //<---Works fine
}
The var m_Id is never set to "", but continues to have the id value.
How can i clear the m_Id inside a const method?

Thank you
 
Hi again

I figured it out! However it might not be the best way. But here goes

CString* aa = (CString*)(&m_Id);
aa->Empty();
 
You could always make m_Id mutable if you need to change it in a const function.

Code:
class obj
{
private:
   [COLOR=blue]mutable[/color] CString m_Id;
...
};

Just use the mutable keyword cautiously and don't overuse it. It should only be used on member variables that don't really affect the state of your object, such as mutexes...
 
Code:
const_cast<CString&>(m_Id).Empty();
Although I agree that mutable is probably a better option.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top