On 17/07/2013 at 03:55, xxxxxxxx wrote:
Originally posted by xxxxxxxx
There has to be a way to do it!!!!!!
I can declare a LONG globally, and there MUST be a way to declare a string too, at least a string containing one single character.
There simply isn't. The constructors of the String class are:
String(void)
{
C4DOS.St->Init(this);
}
String(const String& cs)
{
C4DOS.St->Init(this);
C4DOS.St->CopyTo(&cs, this);
}
String(const UInt16* s)
{
C4DOS.St->Init(this);
StCall(SetUcBlock) (s, -1);
}
String(const Char* cstr, STRINGENCODING type = STRINGENCODING_XBIT)
{
C4DOS.St->Init(this);
C4DOS.St->InitCString(this, cstr, -1, type);
}
String(Int count, UInt16 fillch)
{
C4DOS.St->Init(this);
C4DOS.St->InitArray(this, Int32(count), fillch);
}
C4DOS is a table of function pointers that are passed from Cinema 4D to the plugin on loading.
But the String is constructed before the C4DOS function table is passed to the plugin. And no, you
can not tell the string to construct at a later time without using a pointer and explicitly create an
object on the heap.
Originally posted by xxxxxxxx
I tried the suggestions you submitted, and I am thankful for that! Unfortunately I got a ton of compiler error messages
If you were using the first method I showed you, a pointer to a String, you have to dereference it
in the concatenation:
const String* foo = NULL; // Initialized at a later time
String MethodX() {
return "Have a " + *foo + " nice day!";
}
If you were using the second method, you have to convert it to a C4D String first. A native C string
is just an array of characters ending with a 0 character (so called null-termination). A char is not a
class, it is a POD (Plain old data) type. There are no methods, you can only use its raw value. You
can not add up to pointers, that is why you get a compiler error.
const char* foo = NULL; // Initialized at a later time
String MethodX() {
// char* \+ char* + char* -> INVALID
return "Have a " + foo \+ " nice day!";
}
const char* foo = NULL; // Initialized at a later time
String MethodX() {
// char* \+ String \+ char* -> CORRECT
return "Have a " + String(foo) + " nice day!";
}
The reason this works is the "operator overloading".
Which one you chose depends on your needs. My suggestion is to use a class when you need
a fixed number of strings and more than one. This makes memory management easier.
class StringTable {
public:
String foo;
String bar;
String bat;
String zed;
StringTable() {
foo = "Peter";
bar = "William";
bat = "Daisey";
zed = "Pinocchio";
}
};
const StringTable* strs = NULL;
String MethodX() {
// Either check if `strs` is NULL or make sure this method is never called
// when the globals string table didn't allocate.
return "Hello " + strs->zed;
}
Bool RegisterMyPlugin(); {
if (!strs) {
strs = gNew StringTable;
if (!strs) {
return FALSE; // memory error
}
}
// ...
}
void FreeMyPlugin() {
if (strs) {
gDelete(strs);
strs = NULL;
}
}
Edit: Corrected StringTable class (added public qualifier)