Re: design question
From: Shea Martin (smartin_at_arcis.com)
Date: 08/23/04
- Next message: JohanS: "Get this example to work with member functions?"
- Previous message: Snyke: "Re: Problems inheriting multiple times."
- In reply to: David Hilsee: "Re: design question"
- Next in thread: Alf P. Steinbach: "Re: design question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 23 Aug 2004 08:24:34 -0600
David Hilsee wrote:
> "Shea Martin" <smartin@arcis.com> wrote in message
> news:2on77vFcka4kU1@uni-berlin.de...
> <snip>
>
>>I realize the danger of shallow copies, I was just hoping that there might
>
> way
>
>>to save a deep copy, when I don't need it (without overloading).
>
>
> Are you concerned about performance? For a string class, I would bet that
> the overhead of dynamically allocating the array has more of an adverse
> impact on performance than the actual copy. You could run some tests to
> determine what is the problem. If there is a performance problem with the
> dynamic allocation, have you considered using the old trick where a
> fixed-width buffer is used for smaller strings and a dynamically-allocated
> one is used for larger strings? It might result in cleaner code and more
> efficient behavior for the (usually common) smaller strings. Some
> std::string implementations (Dinkumware's?) do this.
>
> If you wanted to avoid a deep copy, you could pass an extra "bool
> doDeepCopy" to the constructor and cross your fingers that you always
> specify the right value, but that could get tricky.
>
I use a plus or minus system: when allocating I always allocate the amount I
need plus a little more. I also don't bother to shrink my buffer unless the
length of the string varies from the buffer size by a certain threshold.
I realize that the allocate is much slower than the copy. That is why a shallow
copy in certain situations is would avoid an array allocation all together.
Actually what I do now, is have a private method called setBuffer. When a const
char is passed, and I know I won't be altering its contents, I assign the const
char array to the char array member of the string class, and call the same
method using the newly created string. While this approach still requires
overloading, at least it lets me put all my logic in one method instead of doing
copy paste. I still don't really like this method, as it makes me feel dirty.
~S
- Next message: JohanS: "Get this example to work with member functions?"
- Previous message: Snyke: "Re: Problems inheriting multiple times."
- In reply to: David Hilsee: "Re: design question"
- Next in thread: Alf P. Steinbach: "Re: design question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|