ccplusplus.com
Learn C, C++ Concepts
Monday, October 3, 2011
const_cast example c++
/*********************************************************************** * File : const_cast.cpp * Author : Saurabh Gupta * Desc : const_cast example c++ * c++ const_cast example * Source : http://saurabhgupta0527.blogspot.com/p/c_15.html * Created : PM 10:07 03 October 2011 ************************************************************************/ #include
using namespace std; class CConstCast { public: CConstCast () : m_nNumber(0){ } ~CConstCast () { } void m_Set( int ); void m_Display() const; private: int m_nNumber; }; void CConstCast :: m_Set( int nNum ) { m_nNumber = nNum; } void CConstCast :: m_Display() const { cout << "Before Casting\t: " << m_nNumber << endl; const_cast< CConstCast * >( this )->m_nNumber--; cout << "After Casting\t: " << m_nNumber << endl; } int main() { CConstCast g_CCC; g_CCC.m_Set( 8 ); g_CCC.m_Display(); return 0; } /* * OUTPUT * [sgupta@rhel55x86 casting]$ c++ const_cast.cpp -o const_cast [sgupta@rhel55x86 casting]$ ./const_cast Before Casting : 8 After Casting : 7 [sgupta@rhel55x86 casting]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment