Noah said:
Ok, I see where the difference is in the two code segments. You are
mistaking the stack for static variables apparently. Nothing keeps you
from doing this:
void f(CBString & str)
{
static CBString hi = "hi";
str += hi;
}
Right but this is a static, and it executes a construction at init
time.
Besides that I fail to understand what exactly it is you think is
different.
Uhh ... strlen(), etc is called one way or another for CBStrings is
all. I am not a C++ person, so I neglected to consider that C++ allows
for additional init-time function calls (the older portable C standard
doesn't allow this). A static construction, essentially removes the
resource issue, so this makes the point of my example moot.
[...] I have no idea what your functions do beyond your poor
attempt to describe them so there is no way to tell what you are
talking about.
Right ... if only they were open source and documented ... oh wait,
they are.
[...] Simply put, you are completely mistaken about there
being no way to put a variable of any kind on the "stack" in C++. I
don't know how else to put it if you don't understand that. I still
can't figure out why you think otherwise.
Whatever dude. What I am saying is the construction implicitely calls
new/delete (or equivalent) which cannot make a language or runtime
based distinction between a string literal and a string. So, using C
and preprocessor tricks, creating auto-initialization contents with
string literals (that cannot be string variables) is possible. In C++
because construction/destruction is uniform, you can pay a penalty from
extra flags, or are forced to find some other solution to pull the same
sort of trick.
bsStatic(s) is defined as follows:
#define bsStatic(s) { -__LINE__, sizeof("" s "")-1, "" s ""}
(or something like that, I am at a cyber cafe right now so its not
worth it to check it.) As you can see, this takes a string literal,
not a string variable, and it is just a compile time constant
construction. So you can just throw this into a stack variable, and
not worry about when it is going to be deleted, or freed or whatever.
(As a small point, if its non-static, a pure C compiler can fill this
into the auto-variable without holding a static copy in memory. Like,
s.mlen = -__LINE__; s.slen = 0; s.data = ""; without holding the
{-__LINE__, 0, ""} anywhere in memory.)
The use of static in C++ looks like an acceptable alternative stand in
in practice, obviously; presumably, delete need never be called on it.