Board index » delphi » self referencing pointer

self referencing pointer

Hi,

I want to return a pointer to an object-instance from within a method
call. Like:

function MyObject: myobject_ptr;
begin
  result := @self;  { this doesn't work, cause `self' runs out of scope

Quote
}

end;

I could work around it, but that wouldn't make for nice code. I hope
that's not the only solution.

Btw, the reason for this call would be to have a pointer of the same
instance given to an other object. When the original call runs out of
scope the other object still has a valid reference.

Thanks,
Marcel

Sent via Deja.com http://www.deja.com/
Before you buy.

 

Re:self referencing pointer


Hi !

This looks like old TP syntax...
To make it work, omit the @, maybe you'll have to typecast it (don't
remember)...

function MyObject: myobject_ptr;
begin
 result := myobject_ptr(self);  { this doesn't work, cause `self' runs
out of scope
end;

...but it isn't really necessary. A TObject variable in Delphi is a
*pointer* to an object instance.

These two operations are equal:

  Someptr:=SomeInstance.MyObject;
  SomePtr:=myobject_ptr(SomeInstance);

If you're used to TP objects, have a look at "Classes" in the help file.

--

Bjoerge

marce...@my-deja.com skrev i meldingen <80r8sg$ki...@nnrp1.deja.com>...

Quote
>Hi,

>I want to return a pointer to an object-instance from within a method
>call. Like:

>function MyObject: myobject_ptr;
>begin
>  result := @self;  { this doesn't work, cause `self' runs out of scope
>}
>end;

>I could work around it, but that wouldn't make for nice code. I hope
>that's not the only solution.

>Btw, the reason for this call would be to have a pointer of the same
>instance given to an other object. When the original call runs out of
>scope the other object still has a valid reference.

>Thanks,
>Marcel

>Sent via Deja.com http://www.deja.com/
>Before you buy.

Re:self referencing pointer


Maybe something like this, and you could then avoid a function
alltogether:

Type
  TMyObject = class(Tobject)
  .
  .
  end;

  PMyObject = ^TMyObject;

then, when you need a pointer to a particular instance:

procedure MyProcedure;
var P: PMyObject;
begin
  P:=@MyObject1;
end;

The other thing is that, in the case of all non-windowed controls and
most standard windowed controls, the object's value is already a
pointer.

procedure MyProcedure;
var P: Pointer;
begin
  P:=@MyObject1;
end;

Quote
On Tue, 16 Nov 1999 09:41:36 GMT, marce...@my-deja.com wrote:
>Hi,

>I want to return a pointer to an object-instance from within a method
>call. Like:

>function MyObject: myobject_ptr;
>begin
>  result := @self;  { this doesn't work, cause `self' runs out of scope
>}
>end;

>I could work around it, but that wouldn't make for nice code. I hope
>that's not the only solution.

>Btw, the reason for this call would be to have a pointer of the same
>instance given to an other object. When the original call runs out of
>scope the other object still has a valid reference.

>Thanks,
>Marcel

>Sent via Deja.com http://www.deja.com/
>Before you buy.

Re:self referencing pointer


Hi Marcel:

The following complete console program illustrates how to do what you seem
to want--and also that you can do the same thing more simply.

program Project1;
{$APPTYPE CONSOLE}
uses
  SysUtils;

type
  TSimpleObject = class(TObject)
    SomethingToSay: string;
    function identifyMyself: TSimpleObject;
    procedure printSomethingToSay(anOtherSimpleObject: TSimpleObject);
    end;

procedure TSimpleObject.printSomethingToSay(anOtherSimpleObject:
TSimpleObject);
begin
  writeln(anotherSimpleObject.SomethingToSay)
  end;

function TSimpleObject.identifyMyself: TSimpleObject;
begin
  Result:=Self
  end;

var
  SimpleObject1, SimpleObject2: TSimpleObject;

begin
  SimpleObject1:=TSimpleObject.Create;
  with SimpleObject1 do begin
    SomethingToSay:='I''m SimpleObject1';
    end;
  SimpleObject2:=TSimpleObject.Create;
  with SimpleObject2 do begin
    printSomethingToSay(SimpleObject1.identifyMyself);
    printSomethingToSay(SimpleObject1)
    end
  end.

When one runs this program in a DOS box one sees the following:

C:\Program Files\Borland\Delphi5\Projects\reference object>project1.exe
I'm SimpleObject1
I'm SimpleObject1

Note that there are two calls to procedure "printSomethingToSay" and that
each produces the same result. The first one makes a call to a function like
the one you mentioned; the second simply references the object directly.

HTH,

Bill Bell

Quote
marce...@my-deja.com wrote in message <80r8sg$ki...@nnrp1.deja.com>...
>I want to return a pointer to an object-instance from within a method
>call. Like:

>function MyObject: myobject_ptr;
>begin
>  result := @self;  { this doesn't work, cause `self' runs out of scope

Other Threads