|
-
October 16th, 2008, 01:44 PM
#1
const keyword
Hi all,
I come across a question that
whether 'const' keyword is applied to object abstract state or bitwise state.
The answer was that const should be applied to object abstract state.
But, const keyword ensures that object bitwise state is not changed.
I am confused over here. Especially with this example.
class MyString
{
char *m_sz;
public:
MyString( const char *s )
{
m_sz = strdup( s );
}
void toUpper() const
{
char *sz = m_sz;
while( (*sz) != '\0' )
{
(*sz) -= 32;
sz++;
}
}
void Print() const
{
printf( "%s\n", m_sz );
}
};
int main(int argc, char* argv[])
{
const MyString myStr( "hello world" );
myStr.toUpper();
myStr.Print();
return 0;
}
Regards.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|