IonicWind Software

Aurora Compiler => General Discussion => Topic started by: Parker on October 06, 2007, 05:01:54 PM

Title: Can't use class return value
Post by: Parker on October 06, 2007, 05:01:54 PM
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.
Title: Re: Can't use class return value
Post by: Parker on October 07, 2007, 03:07:09 PM
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).
Title: Re: Can't use class return value
Post by: Ionic Wind Support Team on October 07, 2007, 04:01:43 PM
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.
Title: Re: Can't use class return value
Post by: Parker on October 07, 2007, 07:02:58 PM
Thanks.