<<<Index>>>

const member function example


  • Modifying public data members is clearly not possible, but what about calling functions of str?

  • The answer is: the author of the String class specifies which functions of const str can be invoked.

// String.h
class String {
public:
    int length() const;
    void append( char* s );
private:
    int m_length;
    int m_bufSize;
};

// String.cpp
int String::length() const
{
    return m_length;
}

void myfunc( String const& str ) {
    int len = s.length(); // Ok
    s.append( "blah" );   // Compiler error
}
<<<Index>>>