Re: String Reference Type



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?



.



Relevant Pages

  • Re: Complex Specified Information - Pitman Formula
    ... Therefore a significant match between a reference and a test ... string is good evidence of non-random production. ... and there are no finite algorithms to compute their digits. ... probabilities of the different symbols the information source can produce. ...
    (talk.origins)
  • Re: String Reference Type
    ... All unary and binary operators have predefined implementations that are ... Therefore its always allocated in the heap and a variable of string ... As with all classes in this case y and x both reference the same String ... language depandant matter as below. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Abstract class variables question
    ... But as I think you've seen elsewhere in this thread, a value type can exist inside a class and in that case the value type is stored in the heap with the rest of the class instance. ... But as far as the "faster" goes, yes...to some extent value types have less overhead than reference types, and so can perform better in certain cases. ... Well, that would be true for a string object too, if there was any way to actually change a string. ... Seriously though, it is practically always the case that when you are writing an assignment to a reference, you're replacing the reference held by the variable. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Measurement of pitch
    ... as the method used by the Pythagoreans. ... of these reference units in the quantity to be measured. ... vibrating string seems as good as anything. ... The string or pendulum in question could no doubt be specified exactly, ...
    (sci.physics)
  • Re: Abstract class variables question
    ... I think I understand boxing a little better now. ... the object that is on the heap. ... value types are copied to the heap and made into an object and reference ... String types are already reference types and all we are doing when we do ...
    (microsoft.public.dotnet.languages.csharp)