March 29, 2024, 06:13:04 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Can't use class return value

Started by Parker, October 06, 2007, 05:01:54 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Parker

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.

Parker

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).

Ionic Wind Support Team

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.
Ionic Wind Support Team

Parker