[gclist] GC on C++ Strings.

Thomas Weitzel 100335.127@compuserve.com
01 Mar 96 14:22:19 EST


> The one area of C++ where I really do wish I had GC is character
> strings. Has anybody developed a String class GC for C++?

I think what you are looking for is GC by reference counting. This is achieved
easily in C++ by separating the string implementation in two parts: 
1) A body which is allocated from the heap and contains the characters and an
int (the reference count). 
2) A handle which is always an object on the stack (in C++ also called an
"automatic object"). This increments the reference count when copied or assigned
and decrements it on destruction.

There are already many implementations of string classes that use this scheme.
Take any general purpose class library you can access and you are likely to find
a string class implemented in this way.

More information on the handle/body idiom can be found in 
J. Coplien,  "Advanced C++ Programming Styles and Idioms", Addison Wesley 1992
(A very excellent book indeed)

Hope that helps
Tom