Passing Strings (Descriptors) in Functions

Login to reply to this topic.
Sun, 2003-07-13 17:06
Joined: 2003-05-13
Forum posts: 55
Hi,
 I recently found out that if u have to pass descriptors to functions, then u can only do so using a pass by reference or a pointer. Passing directly does not work though no error is reported. So for example, a pass like this Function (TDes& jjjjj) would result in the correct value being passed to jjjjj but Function (TDes jjjjj) would lead to a passing of a blank (kind of) descriptor. This must be due to the fact that originally, in C++, strings are arrays of characters and the string var name is actually a reference to the first location. Am I correct in thinking this?Huh Cheers, coolral.

Sun, 2003-07-13 20:15
Joined: 2003-05-27
Forum posts: 363
Passing Strings (Descriptors) in Functions
Hi there,

What you say is not true in general. I think what you are seeing is a progamming error in your program. My best bet would be to check the places where you use the C++ dereferencing operator ( '*' ) - most likely you are modifying a transparent stack copy of your descriptor instead of the original one. This is valid for any C++ object in general - be careful!  Roll Eyes

Happy coding,
Pawel
Mon, 2003-07-14 09:30
Joined: 2003-05-13
Forum posts: 55
Passing Strings (Descriptors) in Functions
hi pawel,
  well, i am not using the dereferencing operator anywhere in my program. in fact, just before the function call, i checked my TBuf and it showed the correct value. but when i pass my TBuf to the function, and receive it at the function in a TDes, and now when I try to access this TDes, it shows a blank descriptor. However, when I pass using the Address of '&' operator, everything is fine. Can u tell why?Huh thanx, coolral.
Mon, 2003-07-14 10:38
Joined: 2003-05-27
Forum posts: 363
Passing Strings (Descriptors) in Functions
In that case the compiler will create an "invisible" copy of your TBuf on the stack so that the original TBuf is not altered. That is why you should always use references so that your function works on the correct descriptor. Think  of the references as c++ pointers ( they are almost the same thing anyway) and it should be clear.

Happy coding,
Pawel
Mon, 2003-07-14 10:52
Joined: 2003-05-13
Forum posts: 55
Passing Strings (Descriptors) in Functions
hi pawel,
 ok, got ur point. but the problem i was facing was that i passed the descriptor directly without using a reference. so the TDes that i used to receive the TBuf in the function should at least have the same value as the original TBuf even if changing it would not affect the original descriptor. But what I saw was that the TDes which received the value (ie in the function) itself was blank because it was from within that function that I was changing the values of some labels and after that change, the label just showed blanks (not exactly but a few small squares). So why wasn't the value accessible in the function itself. Can u suggest some reason. thanx a lot. cheers, coolral.
Sat, 2006-11-04 18:46
Joined: 2006-11-04
Forum posts: 2
Re: Passing Strings (Descriptors) in Functions
FYI - This might help ...  Afro
===

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).
  • Login to reply to this topic.