Shouldn't TPtr have a default constructor if it encapsulate a pointer?

Login to reply to this topic.
Thu, 2005-08-25 18:09
Joined: 2005-06-13
Forum posts: 68
I was wondering...

Why isn't there a default constructor for TPtr?

What if you wanted to do something like this:


Code:
void method(TPtr& aPtr)
{
   aPtr = _L("blob") ;
}

TPtr ptr ;
method(ptr) ;
// now ptr points to "blob"
The only I can see of doing something similar is like this:


Code:
void method(HBufC*& aBuf)
{
   aBuf = HBufC16::NewL(4) ;
   aBuf->Des() = _L("blob") ;
}

HBufC* buf ;
method(buf) ;
// now buf points to "blob"
Anyone has any better ideas of how do this?

Anyone has a good explanation of why TPtr has no default contructor?

Cheers,

Nikolas.



If we fall down it's so we can learn to pick ourselves up.


Thu, 2005-08-25 18:29
Joined: 2005-07-28
Forum posts: 121
Re: Shouldn't TPtr have a default constructor if it encapsulate
Controls your use of the pointer -  it's only meant to be used to deal with existing blocks of data.

Consider TPtrC, it does have a default constructor, however this class only allows you to read data. TPtrC's default constructor set's it's self to NULL, and length 0. So since your only use of a TPtrC will be read data, if you attempted to read some information from a TPtrC you'd end up with a descriptor of NULL content and zero length. - This makes sense.

Now consider TPtr, it's mean to be used for looking at, and modifying data... if it had a default constructor what would it set it's self too ? NULL and 0 length ? - Ok, but now what happens when you try to write to this descriptor.... you'd get a memory exception, and your app would panic and die - probably with an unhandled exception.

To prevent this, TPtr does not have a default constructor, so you have to set it to something before you can use it.
  • Login to reply to this topic.