Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problems with Object Pascal

I'm also posting this message on the beginners messageboard, though I'm already quite familiar with Delphi. However, the problem described below might be a beginner's problem.

I have a problem writing OO-code in Delphi. How can I use classes which are defined later in the code? Here is an example, what my problem looks like (abstract example):

TObj1 = class
field : TObj2;
end;

TObj2 = class
field : TObj1;
end;

As you can see, changing the order of the declaration will not solve the problem. I was used from writing OO-code in Pascal, that you (normaly) use pointers to objects instead of object vars:

{pascal code}

PObj1 = ^TObj1;
PObj2 = ^TObj2;

TObj1 = object
field : PObj2;
end;

TObj2 = object
field : PObj1;
end;

But since Delphi replaces the need to use pointers to objects simply by using classes (as I mentioned in Delphi, var a : TObject; is actually a pointer to an instance of (an inheritor of) TObject), this doesn't look to be the right way.

Please help me - is there any possibility to declare classes "forward"? Or any other way to do this?

Thank you.

Comments

  • RickListRickList Member Posts: 22
    : I have a problem writing OO-code in Delphi. How can I use classes which are defined later in the code? Here is an example, what my problem looks like (abstract example):
    :
    : TObj1 = class
    : field : TObj2;
    : end;
    :
    : TObj2 = class
    : field : TObj1;
    : end;
    :
    : But since Delphi replaces the need to use pointers to objects simply by using classes (as I mentioned in Delphi, var a : TObject; is actually a pointer to an instance of (an inheritor of) TObject), this doesn't look to be the right way.
    :
    : Please help me - is there any possibility to declare classes "forward"? Or any other way to do this?
    :
    : Thank you.
    :
    Hi there, you can make a forward class declaration.
    [code]
    Type
    TObj2 = class; // Forward declaration of TObj2

    TObj1 = class
    field : TObj2;
    end;

    TObj2 = class
    field : TObj1;
    end;
    [/code]
    HTH

    Richard

  • Justin BibJustin Bib USAMember Posts: 0

    _ \ http://forcoder.org \ free ebooks and video tutorials about \ Objective-C, Visual Basic, Scratch, Go, C#, Swift, C, Visual Basic .NET, Ruby, C++, Assembly, R, MATLAB, Python, Java, PHP, Delphi, Perl, JavaScript, PL/SQL Scala, Fortran, LabVIEW, Awk, Clojure, Kotlin, Alice, Erlang, VBScript, Lua, Logo, COBOL, F#, Apex, Prolog, Transact-SQL, SAS, Lisp, Bash, Ada, D, Dart, ML, Julia, Crystal, ABAP, FoxPro, Scheme, Hack, Rust \ _______

Sign In or Register to comment.