"belief" <su*********@163.com> schrieb im Newsbeitrag news:d5**********@news.yaako.com...
I write a non-template simple string with the technique
that giving the same string a same storage with a count.
so i must give the necessary copy operation when the string will be
modified, I differ the two kinds of operator[] s.
// const object to use as const
const char& operator[] const
{
//directly get the ref
}
char& operator[]
{
// first copy. then give the new copy 's ref
}
but you see, when I just want to get the value ,not to modify it, a
unnecessary copy ocurrs.
Is there some methods to avoid this ?
Thanks.
Don't return a reference. Create your own class, which behaves like a reference, knows which string it refers to, and knows when a copy is required. Something like
class char_ref
{
public:
char_ref(string& base, int offset);
char_ref& operator=(char); // assigns to location refered to,
// copies if neccessary.
operator char() const; // get content of location
...
};
Good luck
Heinz