Board index » delphi » Object Pointers

Object Pointers

I have a problem that I can't seem to figure out.  I want to
get the pointer to an object from inside it's constructor.  
Can that be done?  I've tried using Self, but that's just
implied and not a real variable.  Any ideas?  I plan on calling
the contrucor in a NEW statement..

 

Re:Object Pointers


Quote
bd...@yfn.ysu.edu (Nathan Straz) wrote:

>I have a problem that I can't seem to figure out.  I want to
>get the pointer to an object from inside it's constructor.  
>Can that be done?  I've tried using Self, but that's just
>implied and not a real variable.  Any ideas?  I plan on calling
>the contrucor in a NEW statement..

You should use the address (i.e. @Self) if you are using it in a pointer, like
in:

program Selftest;

type
     pSelfRef = ^SelfRef;
     SelfRef  = object
                   SomeData : byte;
                   pAtSelf : pSelfRef;
                   Constructor Init (aSomeData : byte);
                   procedure ShowData;
                 end;

Constructor SelfRef.Init (aSomeData : byte);
begin
   SomeData := aSomeData;
   pAtSelf := @Self;
end;

procedure ShowData;
begin
   WriteLn ('SomeData: ', SomeData);
end;

var
   pAuto1, pAuto2 : pSelfRef;

begin
   pAuto1 := New (pSelfRef, Init (1));
   pAuto2 := New (pSelfRef, Init (2));
   pAuto1^.pAtSelf^.ShowData;
   pAuto2^.pAtSelf^.ShowData;
   Dispose (pAuto1);
   Dispose (pAuto2);
end.

It prints 1 and 2. :)

Why you'd ever want to do this escapes me, since pAuto1 and pAuto1^.pAtSelf of
course contain the same pointer.

Jos.

Re:Object Pointers


In article <DE0y2H....@dutiws.twi.tudelft.nl>, Jos van Kan <j.van...@math.tudelft.nl> writes:

Quote
> bd...@yfn.ysu.edu (Nathan Straz) wrote:

> >I have a problem that I can't seem to figure out.  I want to
> >get the pointer to an object from inside it's constructor.  
> >Can that be done?  I've tried using Self, but that's just
> >implied and not a real variable.  Any ideas?  I plan on calling
> >the contrucor in a NEW statement..

  I've no idea if it's possible or not, using Self does certainly not work.
  The trouble is that (unless you are in debug mode) self does not get a
  value until you exit the constructor.
  I've had a lot of trouble with this myself, and can see no way of doing it.
  If anyone knows of one I'd be glad to know.

Quote
> You should use the address (i.e. @Self) if you are using it in a pointer, like
> in:

> program Selftest;

> type
>      pSelfRef = ^SelfRef;
>      SelfRef  = object
>                    SomeData : byte;
>                    pAtSelf : pSelfRef;
>                    Constructor Init (aSomeData : byte);
>                    procedure ShowData;
>                  end;

> Constructor SelfRef.Init (aSomeData : byte);
> begin
>    SomeData := aSomeData;
>    pAtSelf := @Self;

                ^^^^^^

  Wrong! Self does not have a value here. If you try doing this with a more
  complex class structure, especially one that has virtual methods, the
  program will crash. I've run into this problem in several different programs
  and have never found a way around it.

- Show quoted text -

Quote
> end;

> procedure ShowData;
> begin
>    WriteLn ('SomeData: ', SomeData);
> end;

> var
>    pAuto1, pAuto2 : pSelfRef;

> begin
>    pAuto1 := New (pSelfRef, Init (1));
>    pAuto2 := New (pSelfRef, Init (2));
>    pAuto1^.pAtSelf^.ShowData;
>    pAuto2^.pAtSelf^.ShowData;
>    Dispose (pAuto1);
>    Dispose (pAuto2);
> end.

> Why you'd ever want to do this escapes me, since pAuto1 and pAuto1^.pAtSelf of
> course contain the same pointer.

  Suppose you want the object to create other objects that should know about
  this object? How would you do that in the constructor when self has no value,
  and the correct value has not yet been assigned to the pointer (pAuto1 or 2)?

  --Lars M.

Re:Object Pointers


In article <41uvrr$...@gyda.ifi.uio.no>, lar...@ifi.uio.no (Lars Marius Garshol) writes...

Quote

>In article <DE0y2H....@dutiws.twi.tudelft.nl>, Jos van Kan <j.van...@math.tudelft.nl> writes:
>> bd...@yfn.ysu.edu (Nathan Straz) wrote:

>> >I have a problem that I can't seem to figure out.  I want to
>> >get the pointer to an object from inside it's constructor.  
>> >Can that be done?  I've tried using Self, but that's just
>> >implied and not a real variable.  Any ideas?  I plan on calling
>> >the contrucor in a NEW statement..

>  I've no idea if it's possible or not, using Self does certainly not work.
>  The trouble is that (unless you are in debug mode) self does not get a
>  value until you exit the constructor.
>  I've had a lot of trouble with this myself, and can see no way of doing it.
>  If anyone knows of one I'd be glad to know.

>> You should use the address (i.e. @Self) if you are using it in a pointer, like
>> in:

>> program Selftest;

>> type
>>      pSelfRef = ^SelfRef;
>>      SelfRef  = object
>>                    SomeData : byte;
>>                    pAtSelf : pSelfRef;
>>                    Constructor Init (aSomeData : byte);
>>                    procedure ShowData;
>>                  end;

>> Constructor SelfRef.Init (aSomeData : byte);
>> begin
>>    SomeData := aSomeData;
>>    pAtSelf := @Self;
>                ^^^^^^

>  Wrong! Self does not have a value here. If you try doing this with a more
>  complex class structure, especially one that has virtual methods, the
>  program will crash. I've run into this problem in several different programs
>  and have never found a way around it.

Errrrrr.... what?
This program works just FINE for me...

{$A-,B-,D+,E-,F-,G+,I-,L+,N-,O+,R-,S-,V+,X-}
{$M 16384,0,655360}
program tries;

type
  Ptry = ^Ttry;
  Ttry = object
           me: Ptry;
           field: word;
           constructor init(thingy: word);
           destructor done; virtual;
         end;

constructor Ttry.init(thingy: word);
begin
  me := @self;
  field := thingy
end;

destructor Ttry.done;
begin
end;

var
  test: Ptry;
begin
  new(test,init(34));
  writeln('Value = ', test^.me^.field);
  dispose(test,done)
end.

This produces the expected result of "Value = 34". It is my understanding that
once the program arrives at the "begin" statement of a constructor then it has
been successfully allocated etc etc. If you wanted to, you could call virtual
methods from inside it.

Chris.

Re:Object Pointers


In article <41uvrr$...@gyda.ifi.uio.no>,
   lar...@ifi.uio.no (Lars Marius Garshol) wrote:
Quote

>In article <DE0y2H....@dutiws.twi.tudelft.nl>, Jos van Kan
<j.van...@math.tudelft.nl> writes:
>> bd...@yfn.ysu.edu (Nathan Straz) wrote:

>> >I have a problem that I can't seem to figure out.  I want to
>> >get the pointer to an object from inside it's constructor.  
>> >Can that be done?  I've tried using Self, but that's just
>> >implied and not a real variable.  Any ideas?  I plan on calling
>> >the contrucor in a NEW statement..

I think the only reasonable way is to define the pointer as the value of the
New statement. That way, too, it's often more convenient to have the desired
scope.
That is,
var
  ptrVar:ObjectPointerType;
..
ptrVar:=new(ObjectPointerType,init(...));

Bruce Winterbon
bwint...@intranet.on.ca
http://intranet.on.ca/~bwinterb

Re:Object Pointers


In article <41oi6l$...@news.ysu.edu>, bd...@yfn.ysu.edu says...

Quote

>I have a problem that I can't seem to figure out.  I want to
>get the pointer to an object from inside it's constructor.  
>Can that be done?  I've tried using Self, but that's just
>implied and not a real variable.  Any ideas?  I plan on calling
>the contrucor in a NEW statement..

* Self ain't a pointer. To get the pointer, use @Self (if it ain't that
what you tried)

Re:Object Pointers


In article <41oi6l$...@news.ysu.edu>, bd...@yfn.ysu.edu says...

Quote

>I have a problem that I can't seem to figure out.  I want to
>get the pointer to an object from inside it's constructor.  
>Can that be done?  I've tried using Self, but that's just
>implied and not a real variable.  Any ideas?  I plan on calling
>the contrucor in a NEW statement..

Try using @Self (Self ain't a pointer, it is the object) (I hope it ain't
that what you tried)

Other Threads