473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
Jul 23 '05 #1
3 1619
"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
Jul 23 '05 #2
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" <ho**********@arcor.de> ????
news:42***********************@newsread4.arcor-online.net...
"belief" <su*********@163.com> schrieb im Newsbeitrag
news:d5**********@news.yaako.com...

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
Jul 23 '05 #3

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

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3046
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
2
4694
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
0
1395
by: Dan Perl | last post by:
I have posted this problem also on the wxpython-users list, but maybe someone here can also help. I want to keep 2 different views for the same object, both views using SplitterWindow. So I am...
8
1530
by: Dan Perl | last post by:
Here is a problem I am having trouble with and I hope someone in this group will suggest a solution. First, some code that works. 3 classes that are derived from each other (A->B->C), each one...
0
2338
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
0
1621
by: Daniel Crespo | last post by:
Hi to all, I'm using adodb for accessing mysql and postgres. My problem relies on the mysql access. Sometimes, when I try to execute a query (using ExecTrans method below), I get this error:...
5
1220
by: alainpoint | last post by:
Hi, I have what in my eyes seems a challenging problem. Thanks to Peter Otten, i got the following code to work. It is a sort of named tuple. from operator import itemgetter def...
3
2346
by: aldonnelley | last post by:
Hi all. Just having a weird problem with tkinter. I'm trying to make a gui that shows results from an image search, with a "forward" and "back" button so the user can compare results from...
4
1989
by: amidzic.branko | last post by:
I'm trying to solve a problem using inheritance and polymorphism in python 2.4.2 I think it's easier to explain the problem using simple example: class shortList:
7
3466
Thekid
by: Thekid | last post by:
How can I go about writing a simple IRC bot? I read one thread in this forum about an IRC bot but I couldn't get it to work. I've looked at a few like eggdrop, supybot and phenny but I want to try a...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.