|
|
User login
Feeds |
Returning TBuf as automatic variable - not possible?
|
|||||
| Tue, 2004-11-23 09:41 | |
|
I have a method in which I define a TBuf<20> (stack-based). I want to return exactly this Tbuf, as an automatic, stack-based copy. Simple and plain... but it doesn't work! It seems that my data in the descriptor gets mixed up somehow, you can see some things while debugging, but it's not an identical copy!
I did it like this way: TDes foo() { TBuf<20> output; //... output gets properly (!) filled return output; } called like this: TBuf<20> input; input = foo(); I don't want to use references, because these TBufs (in different classes) should be totally independent and merely copies. What's wrong? I've tried almost everything it seems to me! The problem maybe is related to http://forum.newlc.com/viewtopic.php?t=172&highlight=return+tbuf |
|
Forum posts: 723
Another solution:
{
TInt error = KErrNone;
// Here you have to fill aBuffer properly
// If an error occured, handle it by using 'error' variable properly
return error;
}
TBuf<20> input;
User::LeaveIfError( GetFoo( input ) );
It's that simple.
Cheers,
tOtE
Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/
Forum posts: 20
BUT anyway - I wonder why you can return the TInt you defined in the function easily - doesn't it go out of scope, too (because you said:"you must NEVER return a local variable") ? What's the difference between the TInt and let's say my TBuf? Both are a local variable!? I always thought it's just a copy that gets passed! (??)
Forum posts: 723
If you want to keep the original behavior, then you will have to slightly modify the return value from TDes to TBuf. Like:
{
TBuf<20> output;
// ...
return output;
}
That would ensure that a copy of the whole buffer will be returned.
Is it clear now?
tOtE
Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/
Forum posts: 20
Somehow the SDK gave me not that presentation of things you did
BTW: The second variant is not so fast as the first one (passing by reference), isn't it?
Forum posts: 723
You're right. Passing a reference means passing a pointer, which means - in a 32-bit architecture - 4 bytes.
Gabor Torok
Software architect, Agil Eight (http://www.agileight.com/)
Blog: http://mobile-thoughts.blogspot.com/
Forum posts: 33
I did it like this way:
TDes foo()
{
TBuf<20> output;
//... output gets properly (!) filled
return output;
}
called like this:
TBuf<20> input;
input = foo();
I don't want to use references, because these TBufs (in different classes) should be totally independent and merely copies.
What's wrong?
You could of course write something like this:
{
TBuf<20> output;
//
return output;
}
Regards, Bo
Forum posts: 2
===
from: http://descriptors.blogspot.com/2005/05/9-how-do-i-use-descriptors-as.html
My function uses TDes or TDesC parameters like you say, but my descriptor passing doesnÂ’t work. Why not?
Oh how much pain has this one caused? Missing out that little & symbol makes all the difference. Your parameter types must be references, not values, ie const TDesC& or TDes&.
The base classes TDesC and TDes contain no string data. If you pass them by value rather than by reference, you are using static binding, which means that polymorphism wonÂ’t work, and you'll end up with a data-free base class object. It will all compile OK, but nothing works.
Never attempt to instantiate or work directly with objects of the base classes TDesC or TDes, as Tip 1 advises. They are effectively abstract classes. There is rarely, if ever, a valid reason for instantiating them rather than an object of their deriving classes (TBufC, TBuf, TPtrC, TPtr, HBufC or RBuf).
Forum posts: 33
Never attempt to instantiate or work directly with objects of the base classes TDesC or TDes, as Tip 1 advises. They are effectively abstract classes. There is rarely, if ever, a valid reason for instantiating them rather than an object of their deriving classes (TBufC, TBuf, TPtrC, TPtr, HBufC or RBuf).
The only possible reason would be to just keep know the length descriptor, TDesC, or length and max length, TDes. Any use of the instance would render undefined behaviour.
But still, NEVER instantiate them! In the future any code with concrete TDesC/TDes variables will not compile. Check it out by adding a protected copy constructor to TDesC and TDes (inline implementation needed of course).
Regards, Bo