Problem about self-string operator[] 's unnecessary copy

B

belief

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.
 
H

Heinz Ozwirk

belief said:
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
 
W

wisdo

Hi,Heinz.

It's a good solution ! by giving a intermediate layer .
Maybe char_ref will give me more work to implement it's behavior
like inner type char, i.e. operator= operator+= opeartor*= operator/= etc.

As the solution , the necessary copy operation will be separated to two
pieces.
some ones in the String(e,g, insert, replace) ,and others in the char_ref
like operators just talking above, isn't it?

I think i get it .
Thanks a ton.

wisdo (belief).

//--------------------------------------------------------------------------
----------------------------------------------------------------------------
-

"Heinz Ozwirk" <[email protected]> ????

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
 
M

msalters

belief schreef:
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
}

It's probably more efficient to return a plain char. What's the point
in having a reference? That also prevents the following bug:

SimpleString a = "foo";
SimpleString const& b = a;
char const& f = b[0];
a[0]='x';
assert(f=='f');
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.

References are dumb (like pointers), use a smart ref class.

Regards,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top