Pregunta

The goal is to add a list of edges to a graph using instances of TList. TList is a standard list class in FreePascal, with an iterator interface and other creature comforts.

Here is my idea in FreePascal code:

PROGRAM PassPointerToClass;
TYPE
  class C
  // ...
  public
    F : TList;
  end; // end of class definition

VAR
  P : TList;
  i : integer;

BEGIN
  O := C.Create;
  i := 2;
  P := TList.Create;
  P.Add(@i);
  O.F := P;
  P := Nil;
END.

Is

  1. F=Nil
  2. F^[0]=2, or
  3. something else?
¿Fue útil?

Solución

P is a pointer to a heap allocated class. O.F is assigned the same value as P.

Setting P to nil does not affect the allocated heap object.

O.F still points to this object and F[0]^ is 2.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top