Recent

Author Topic: Accessing Parent/Child class functions  (Read 7623 times)

antsnest

  • Newbie
  • Posts: 5
Accessing Parent/Child class functions
« on: July 01, 2011, 10:41:46 am »
I've got the followin g data structure

Tobject1 = class(tobject)
  id : integer;
  name : string;
private
public
  procedure describemyself(num);
end;

Tobject2 = class(tobject)
  id : integer;
private
  theobjects : array[1..10] of tobject1;
public
  procedure describeall;
  function findobjectbyname(name : string);
end;

procedure tobject2.describeall;
begin
  for l:=1 to 10 do
    theobjects[l].describemyself;
end;

procedure  tobject1.describemyself(num : integer);
begin
**  access object2  findobjectbyname here **
end;

Assuming that all the object creation routines and the undefined findobjectby name are correct is there any way I can access the object 2 function from within onject one as shown?

The actual reason I want to use it is similar to the above, I have a bunch of objects held in an array which reference each other by ID. Mostly I'm accessing this data from 2 levels up whch means I just reference the structure for the function and the data, at one poin t however it'd be really useful to access thigs at a low level.

I can code round it by moving functyions up a level or two but it's not as elegant.

Tim

Arbee

  • Full Member
  • ***
  • Posts: 223
Re: Accessing Parent/Child class functions
« Reply #1 on: July 02, 2011, 10:45:38 pm »
In TObject1 you could store a reference to the parent TObject2 object, which you would fill when you create the TObject1 instances (I don't see any creates in your coding by the way).

So you could have a variable

Code: [Select]
theParent:  TObject2

in the TObject1 class.

There then could add a constructor to TObject1 which accepts a TObject2 and store this in theParent

Code: [Select]
constructor TObject1.create(parent: TObject2);
begin
   theParent := parent;
end;

and then you can access the function by calling

Code: [Select]
theParent.FindObjectByName(...);

But again ... you have to add some "creates" here and there in your program.
« Last Edit: July 02, 2011, 10:48:13 pm by Arbee »
1.0/2.6.0  XP SP3 & OS X 10.6.8

eny

  • Hero Member
  • *****
  • Posts: 1653
Re: Accessing Parent/Child class functions
« Reply #2 on: July 03, 2011, 01:00:22 am »
I can code round it by moving functyions up a level or two but it's not as elegant.
Use an abstract base class, interfaces, RTTI etc.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Accessing Parent/Child class functions
« Reply #3 on: July 03, 2011, 10:56:16 am »
Your design is really weird. TObject2 seems like a container for TObject1, but then you need to access TObject2's members from TObject1. Nevertheless, Arbee points a solution. Additionally, you have to forward the declaration of TObject2 since TObject1 and TObject2 are mutually referencing.

 

TinyPortal © 2005-2018