While working on a string class, I discovered that this doesn't work:
void returnsClass( ),classtype
{
classtype ct;
return ct;
}
...
classtype ct;
ct = returnsClass( );
Thinking the = might not be doing what I want it to, I tried this: (my string class has Set methods)
classtype ct;
ct.Set( returnsClass( ) );
Same problem - the program crashes :(
This makes it hard to work with dynamic strings. I really can't be using pointers in this case, because then the end user would have to delete them after he has called the function. Imagine this:
MyString hello, world;
hello.SetStr( "Hello" );
world.SetStr( "World" );
MyString helloworld;
MyString *ptr = hello.AddChar( ' ' );
MyString *ptr2 = ptr->Add( world );
helloworld.Set( *ptr2 );
delete ptr;
delete ptr2;
I'm trying for this:
MyString hello, world;
hello.SetStr( "Hello" );
world.SetStr( "World" );
MyString helloworld;
helloworld.Set( hello.AddChar( ' ' ).Add( world ) );
or at least this:
MyString hello, world;
hello.SetStr( "Hello" );
world.SetStr( "World" );
MyString helloworld;
helloworld.Set( hello.AddChar( ' ' ) );
helloworld.Append( world );
Preferably the first form where I can use Add().Add().Add()... to add new strings on to the ends of temporary strings. I'm trying to get this as user friendly as I can, and the pointer way is just not user friendly at all. Might as well handle your own dynamic strings with new/delete.
I've diagnosed the problem: the destructor is being called before the class is returned, which causes the class (a dynamic string class) to delete the string pointer. Then when I try to assign that string to another variable, it doesn't exist anymore.
I think the solution to this is to call the destructor after returning from the function (the function does not call the destructor; the caller does).
Parker,
I've noted the problem. But it will be at least a few months with my current project before I have any time to address it.
And deleting a pointer isn't very difficult, us end users from the C world are quite used to it ;)
Paul.
Thanks.