Re: String Reference Type
- From: "Lars" <jon.doe@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 09 Feb 2008 23:16:39 GMT
Hi
No, Strings in Pascal (and ADA as I reacll) are tru types. When Delphi
started uses longer strings thye implemented it with a reference but you
never see that. You can still use call by value for a 3456 character long
string. But the string is probably copied into a temporarilly string much as
a ansi C++ template string would do with the default and copy contructors.
String myStringFunc( S: String )
begin
myStringFunc := S + S;
(* If S is longer than 128 chars then the returning string is
cut at pos 255 (position) holds the length of the string.
Parameter S is unchanged since it is a call by value and the
parameter
is copied into S which is on the stack
*)
end;
String myStringFunc( var S: String )
begin
myStringFunc := S + S;
(* If S is longer than 128 chars then the returning string is
cut at pos 255 (position) holds the length of the string.
Parameter S is canged since it is a call by referencek
*)
end;
C++ code
//
// String is a predefined class written some where
//
// I recall Visal C++ uses CString for strings and BC Builder uses
// same strings types as Delphi since BC Builders VCL code is written
// in Delphi. Both BCB and Delphi uses the same backend compiler
//
String myFunc( String S )
{
return S & S;
// Weather the values in the input parameter is changed or no
// depends on how the copycontructor and default contructos
// is defined. Weather the class String uses deep copy or shallow copy
// for the constructors.
//
// If the copy contructor inludes s reference member in stead of a
pointer
// char& in stead of char*
// This code fails to compile unless you have written the default and
copy contructors.
// THAT IS WHYYOU ALWAYS SHOULD USE refernences "&" NOT pointerrs
// "*" IN C++ CLASSES!
// Then you MUST define the default and copy constructors.
//
}
String& myFunc( String& S )
{
return S & S;
// S is changed weather the actual storing is changed or not depends
// on how the copy contructor was written (constructor String( String&
S ); )
// The result is copied into another string object. How that is done is
also
// depndant of now the copy contructor was writte.
// If the copy contructor inludes s reference member in stead of a
pointer
// char& in stead of char*
// This code can be copmiled. The paremeter in is the parameted send
out
// same as String* myFunc( String* S );
}
Don't know about C# but I hope the compiler takes care of this. By
references or not is not of interest. What is of interest is if you have the
option to use call by value for strings or if all parameters are treated as
references. Can you use call by value in C# how do you use call by value?
Lars
"Jonathan Wood" <jwood@xxxxxxxxxxxxxxxx> skrev i meddelandet
news:e02bbg2aIHA.5900@xxxxxxxxxxxxxxxxxxxxxxx
A string can potentially contain a lot of data, and there's no telling what
size a particular string might need to be.
Therefore, not only does it make little sense to try and store a string in
a register or on the stack, or anything else you'd do with a value type,
strings have always been pointers in traditional languages.
Why would it be a value type?
--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com
"RN1" <rn5a@xxxxxxxxxxxxxx> wrote in message
news:ed9a9b74-a8c9-47f4-ab87-38a6b3234b1c@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
If I am not mistaken, String is reference type. Can someone please
explain me why is it reference type & not value type?
.
- Follow-Ups:
- Re: String Reference Type
- From: Jonathan Wood
- Re: String Reference Type
- References:
- String Reference Type
- From: RN1
- Re: String Reference Type
- From: Jonathan Wood
- String Reference Type
- Prev by Date: Re: String Reference Type
- Next by Date: Re: String Reference Type
- Previous by thread: Re: String Reference Type
- Next by thread: Re: String Reference Type
- Index(es):
Relevant Pages
|