Board index » delphi » reference to class or pointer to class?

reference to class or pointer to class?

Example:
        A=class;
        B=class(A);
        C=class(A);

...
var
  aa1: A;
  aa2: ^A;   { <== is aa2 a "pointer to a class" or
                "pointer to reference to class"?}
  bb: B;
  cc: C;

  the problem is :
        How can I set (aa1=bb) or (aa1=cc) by using the aa2 ???

thanks.

J.Y.Pu.

 

Re:reference to class or pointer to class?


Quote
u8217...@cc.nctu.edu.tw () wrote:
>Example:
>    A=class;
>    B=class(A);
>    C=class(A);

>....
>var
>  aa1: A;
>  aa2: ^A;   { <== is aa2 a "pointer to a class" or
>            "pointer to reference to class"?}
>  bb: B;
>  cc: C;

>  the problem is :
>    How can I set (aa1=bb) or (aa1=cc) by using the aa2 ???

>thanks.

>J.Y.Pu.

Well, you could write
aa2^ := bb;
aa1 := aa2^;

But why not just write
aa1 := bb;
?
As I understand it, your variables aa1, bb and cc are references
to classes (i.e. pointers), so your aa2 would indeed be a pointer
to a reference (or pointer).
You don't really need to be bothered by this in order to use it,
however.  The only time it makes a difference is if you
were to destroy aa1 or bb after making the assignment
we're talking about.  In this case, your other reference to
the object would become invalid at the time you destroyed
the first.
If this worries you, you'd have to write a deep-copy method
to assign the contents of one object to another.

-----------------------------------
Matt Francomb, Setanta Software Ltd
http://www.demon.co.uk/setanta
-----------------------------------

Re:reference to class or pointer to class?


Quote
m...@setanta.demon.co.uk (Matt Francomb) wrote:
>u8217...@cc.nctu.edu.tw () wrote:
>>Example:
>>        A=class;
>>        B=class(A);
>>        C=class(A);

>>....
>>var
>>  aa1: A;
>>  aa2: ^A;   { <== is aa2 a "pointer to a class" or
>>                "pointer to reference to class"?}
>>  bb: B;
>>  cc: C;

>>  the problem is :
>>        How can I set (aa1=bb) or (aa1=cc) by using the aa2 ???

>>thanks.

>>J.Y.Pu.

>Well, you could write
>aa2^ := bb;
>aa1 := aa2^;

>But why not just write
>aa1 := bb;
>?
>As I understand it, your variables aa1, bb and cc are references
>to classes (i.e. pointers), so your aa2 would indeed be a pointer
>to a reference (or pointer).
>You don't really need to be bothered by this in order to use it,
>however.  The only time it makes a difference is if you
>were to destroy aa1 or bb after making the assignment
>we're talking about.  In this case, your other reference to
>the object would become invalid at the time you destroyed
>the first.
>If this worries you, you'd have to write a deep-copy method
>to assign the contents of one object to another.

As a part of the Object Pascal language spec, ALL class instances are ALWAYS references
(pointers).  You should therefore rarely, if ever, need to do something like
^SomeClassInstance.

     -Steve Teixeira
      steix...@borland.com

Re:reference to class or pointer to class?


I have located a bug when dealing with MDI child forms.  The Position property
is ignored in an MDI child form, at least when the form is instantiated at
run-time.  I have a small listbox window that should only be 210 pixels wide,
but shows up at the default MDI size/position regardless of the setting in the
Position property.

The only workaround I've found so far is to adjust the Top, Left, Width and
Height properties from the OnCreate event of the MDI child.  However, this is
most inconvenient because I can't adjust the form with the visual design tools
and have the changes stick.

Is this a known bug or is there some reason that the Position property is
ignored?

--------------------
Matt Palcic
JAYCOR Environmental
--------------------

Other Threads