This is actually really common in C++ libraries. I even worked in one (which I know was on Windows, but I don't think was MFC; maybe OWL?) where the pointers to the String object were actually pointing directly to the null-terminated C string, and then each function in the class would start by fixing up the pointer. E.g., something like
class StringClass {
...all members, and then...
char *str;
};
StringClass *StringClass::new() {
StringClass st = new StringClass;
...
return (StringClass *)st->str;
}
void StringClass::someFunc() {
StringClass *realThis = this - sizeof(StringClass) + sizeof(char *);
...
}
Evil, eh?
The good news is that, no, this does not have to prevent you having nulls in your string. The way this usually works is that all of the language's native string handling routines just use the length, so that's fine, and you're at least protected if you want to call C string handling routines instead. This can result in some unexpected behavior on the C side, but it beats the alternative.
The good news is that, no, this does not have to prevent you having nulls in your string. The way this usually works is that all of the language's native string handling routines just use the length, so that's fine, and you're at least protected if you want to call C string handling routines instead. This can result in some unexpected behavior on the C side, but it beats the alternative.