[Click] Can't use String::c_str on const String

Eddie Kohler kohler at icir.org
Tue Oct 14 10:35:14 EDT 2003


Hi Matt,

> If I declare a variable as type "String const", GCC will not let me use the 
> "c_str" member function:
> 
> "passing `const String' as `this' argument
> of `const char *String::c_str ()' discards qualifiers"
> 
> I believe this can be fixed by declaring "String::c_str" as a "const" member 
> function.  That is how "std::string:c_str" is declared:
> 
> const char *cc() const; // pointer returned is semi-transient
> const char *c_str() const { return cc(); }

Can anyone think of any reason why String::c_str() should not be const?
It's not const now because String::c_str() actually changes the data
representation. But I tend to think we could make it const without any
problems in practice.

In the meantime, use the following incantation to avoid the error:

    const String &a;
    ... String(a).c_str() ...

Eddie


More information about the click mailing list