Function returning HBufC*
| Thu, 2008-01-17 18:12 | |
|
Hello all If yes, what would be the recommended way to to use functions like this directly (without assigning to a variable)? Thank you
void CMyApp::DoSomething()
{
if (aTBufVar.Find(GetSearchVal()->Des()) == 0)
{
//Do something
}
}
HBufC* CMyApp::GetSearchVal()
{
HBufC* buf = HBufC::NewL(20);
//Put a value in buf
return buf;
}
|
|






Forum posts: 14
First you need to assign that HBufC to a variable and after using it, it needs to be deleted.
Forum posts: 49
You can write something like this
class CMyApp:
{
...
HBufC* iLastSearchVal;
...
};
void CMyApp::DoSomething()
{
if (aTBufVar.Find(GetSearchVal()->Des()) == 0)
{
//Do something
}
}
HBufC* CMyApp::GetSearchVal()
{
if ( iLastSearchVal != NULL )
{
delete iLastSearchVal;
iLastSearchVal = NULL;
}
iLastSearchVal = HBufC::NewL(20);
//Put a value in buf
return iLastSearchVal;
}
CMyApp::~CMyApp()
{
...
if ( iLastSearchVal != NULL )
{
delete iLastSearchVal;
iLastSearchVal = NULL;
}
...
}
But you should avoid such coding style.
Forum posts: 145
Thank you very much kriger and lman !
Forum posts: 14
Ok, the most tidy solution would be
void CMyApp::DoSomethingL()
{
HBufC* myDescriptor = GetSearchValLC();
if (aTBufVar.Find(myDescriptor ->Des()) == 0)
{
//Do something
}
CleanupStack::PopAndDestroy( myDescriptor );
}
HBufC* CMyApp::GetSearchValLC()
{
HBufC* buf = HBufC::NewLC(20);
//Put a value in buf
return buf;
}
Forum posts: 145
Thank you kriger
If I understand correctly, your suggestion would also work without assigning the variable and with using PopAndDestroy() , right?
void CMyApp::DoSomethingL() { if (aTBufVar.Find(GetSearchValLC() ->Des()) == 0) { //Do something } CleanupStack::PopAndDestroy( ); } HBufC* CMyApp::GetSearchValLC() { HBufC* buf = HBufC::NewLC(20); //Put a value in buf return buf; }Forum posts: 14
Yes, it will work but it is more readable and you know what object are popping.